基于Python編寫一個(gè)圖片識(shí)別系統(tǒng)_第1頁
基于Python編寫一個(gè)圖片識(shí)別系統(tǒng)_第2頁
基于Python編寫一個(gè)圖片識(shí)別系統(tǒng)_第3頁
基于Python編寫一個(gè)圖片識(shí)別系統(tǒng)_第4頁
基于Python編寫一個(gè)圖片識(shí)別系統(tǒng)_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第基于Python編寫一個(gè)圖片識(shí)別系統(tǒng)#

遍歷每個(gè)

self.merge_regions

的元素

for

index,

region

in

enumerate(self.merge_regions):

#

遍歷元素中的每個(gè)區(qū)域號(hào)

for

r_index

in

region:

if

r_index

==

_from:

from_index

=

index

if

r_index

==

_to:

to_index

=

index

#

若兩個(gè)區(qū)域號(hào)都存在于

self.merge_regions

if

from_index

!=

-1

and

to_index

!=

-1:

#

如果這兩個(gè)區(qū)域號(hào)分別存在于兩個(gè)列表中

#

那么合并這兩個(gè)列表

if

from_index

!=

to_index:

self.merge_regions[from_index].extend(self.merge_regions[to_index])

del(self.merge_regions[to_index])

return

#

若兩個(gè)區(qū)域號(hào)都不存在于

self.merge_regions

if

from_index

==

-1

and

to_index

==

-1:

#

創(chuàng)建新的區(qū)域號(hào)列表

self.merge_regions.append([_from,

_to])

return

#

若兩個(gè)區(qū)域號(hào)中有一個(gè)存在于

self.merge_regions

if

from_index

!=

-1

and

to_index

==

-1:

#

將不存在于

self.merge_regions

中的那個(gè)區(qū)域號(hào)

#

添加到另一個(gè)區(qū)域號(hào)所在的列表

self.merge_regions[from_index].append(_to)

return

#

若兩個(gè)待合并的區(qū)域號(hào)中有一個(gè)存在于

self.merge_regions

if

from_index

==

-1

and

to_index

!=

-1:

#

將不存在于

self.merge_regions

中的那個(gè)區(qū)域號(hào)

#

添加到另一個(gè)區(qū)域號(hào)所在的列表

self.merge_regions[to_index].append(_from)

return

在序列中循環(huán)時(shí),索引位置和對(duì)應(yīng)值可以使用enumerate()函數(shù)同時(shí)得到,在上面的代碼中,索引位置即為index,對(duì)應(yīng)值即為region。self._merge()方法則是將self.merge_regions中的元素中的區(qū)域號(hào)所代表的區(qū)域合并,得到新的皮膚區(qū)域列表:

def

_merge(self,

detected_regions,

merge_regions):

#

新建列表

new_detected_regions

#

其元素將是包含一些代表像素的

Skin

對(duì)象的列表

#

new_detected_regions

的元素即代表皮膚區(qū)域,元素索引為區(qū)域號(hào)

new_detected_regions

=

[]

#

merge_regions

中的元素中的區(qū)域號(hào)代表的所有區(qū)域合并

for

index,

region

in

enumerate(merge_regions):

try:

new_detected_regions[index]

except

IndexError:

new_detected_regions.append([])

for

r_index

in

region:

new_detected_regions[index].extend(detected_regions[r_index])

detected_regions[r_index]

=

[]

#

添加剩下的其余皮膚區(qū)域到

new_detected_regions

for

region

in

detected_regions:

if

len(region)

0:

new_detected_regions.append(region)

#

清理

new_detected_regions

self._clear_regions(new_detected_regions)

#

添加剩下的其余皮膚區(qū)域到

new_detected_regions

for

region

in

detected_regions:

if

len(region)

0:

new_detected_regions.append(region)

#

清理

new_detected_regions

self._clear_regions(new_detected_regions)

self._clear_regions()方法只將像素?cái)?shù)大于指定數(shù)量的皮膚區(qū)域保留到self.skin_regions:

#

皮膚區(qū)域清理函數(shù)

#

只保存像素?cái)?shù)大于指定數(shù)量的皮膚區(qū)域

def

_clear_regions(self,

detected_regions):

for

region

in

detected_regions:

if

len(region)

30:

self.skin_regions.append(region)

self._analyse_regions()是很簡(jiǎn)單的,它的工作只是進(jìn)行一系列判斷,得出圖片是否色情的結(jié)論:

#

分析區(qū)域

def

_analyse_regions(self):

#

如果皮膚區(qū)域小于

3

個(gè),不是色情

if

len(self.skin_regions)

3:

self.message

=

"Less

than

3

skin

regions

({_skin_regions_size})".format(

_skin_regions_size=len(self.skin_regions))

self.result

=

False

return

self.result

#

為皮膚區(qū)域排序

self.skin_regions

=

sorted(self.skin_regions,

key=lambda

s:

len(s),

reverse=True)

#

計(jì)算皮膚總像素?cái)?shù)

total_skin

=

float(sum([len(skin_region)

for

skin_region

in

self.skin_regions]))

#

如果皮膚區(qū)域與整個(gè)圖像的比值小于

15%,那么不是色情圖片

if

total_skin

/

self.total_pixels

*

100

15:

self.message

=

"Total

skin

percentage

lower

than

15

({:.2f})".format(total_skin

/

self.total_pixels

*

100)

self.result

=

False

return

self.result

#

如果最大皮膚區(qū)域小于總皮膚面積的

45%,不是色情圖片

if

len(self.skin_regions[0])

/

total_skin

*

100

45:

self.message

=

"The

biggest

region

contains

less

than

45

({:.2f})".format(len(self.skin_regions[0])

/

total_skin

*

100)

self.result

=

False

return

self.result

#

皮膚區(qū)域數(shù)量超過

60個(gè),不是色情圖片

if

len(self.skin_regions)

60:

self.message

=

"More

than

60

skin

regions

({})".format(len(self.skin_regions))

self.result

=

False

return

self.result

#

其它情況為色情圖片

self.message

=

"Nude!!"

self.result

=

True

return

self.result

然后可以組織下分析得出的信息:

def

inspect(self):

_image

=

'{}

{}

{}×{}'.format(self.image.filename,

self.image.format,

self.width,

self.height)

return

"{_image}:

result={_result}

message='{_message}'".format(_image=_image,

_result=self.result,

_message=self.message)

Nude類如果就這樣完成了,最后運(yùn)行腳本時(shí)只能得到一些真或假的結(jié)果,我們需要更直觀的感受程序的分析效果,我們可以生成一張?jiān)瓐D的副本,不過這個(gè)副本圖片中只有黑白色,白色代表皮膚區(qū)域,那么這樣我們能直觀感受到程序分析的效果了。

前面的代碼中我們有獲得圖像的像素的RGB值的操作,設(shè)置像素的RGB值也就是其逆操作,還是很簡(jiǎn)單的,不過注意設(shè)置像素的RGB值時(shí)不能在原圖上操作:

#

將在源文件目錄生成圖片文件,將皮膚區(qū)域可視化

def

showSkinRegions(self):

#

未得出結(jié)果時(shí)方法返回

if

self.result

is

None:

return

#

皮膚像素的

ID

的集合

skinIdSet

=

set()

#

將原圖做一份拷貝

simage

=

self.image

#

加載數(shù)據(jù)

simageData

=

simage.load()

#

將皮膚像素的

id

存入

skinIdSet

for

sr

in

self.skin_regions:

for

pixel

in

sr:

skinIdSet.add(pixel.id)

#

將圖像中的皮膚像素設(shè)為白色,其余設(shè)為黑色

for

pixel

in

self.skin_map:

if

pixel.id

not

in

skinIdSet:

simageData[pixel.x,

pixel.y]

=

0,

0,

0

else:

simageData[pixel.x,

pixel.y]

=

255,

255,

255

#

源文件絕對(duì)路徑

filePath

=

os.path.abspath(self.image.filename)

#

源文件所在目錄

fileDirectory

=

os.path.dirname(filePath)

+

'/'

#

源文件的完整文件名

fileFullName

=

os.path.basename(filePath)

#

分離源文件的完整文件名得到文件名和擴(kuò)展名

fileName,

fileExtName

=

os.path.splitext(fileFullName)

#

保存圖片

simage.save('{}{}_{}{}'.format(fileDirectory,

fileName,'Nude'

if

self.result

else

'Normal',

fileExtName))

變量skinIdSet使用集合而不是列表是有性能上的考量的,Python中的集合是哈希表實(shí)現(xiàn)的,查詢效率很高。最后支持一下命令行參數(shù)就大功告成啦!我們使用argparse這個(gè)模塊來實(shí)現(xiàn)命令行的支持。argparse模塊使得編寫用戶友好的命令行接口非常容易。程序只需定義好它要求的參數(shù),然后argparse將負(fù)責(zé)如何從sys.argv中解析出這些參數(shù)。argparse模塊還會(huì)自動(dòng)生成幫助和使用信息并且當(dāng)用戶賦給程序非法的參數(shù)時(shí)產(chǎn)生錯(cuò)誤信息。

具體使用方法請(qǐng)查看argparse的官方文檔

if

__name__

==

"__main__":

import

argparse

parser

=

argparse.ArgumentParser(description='Detect

nudity

in

images.')

parser.add_argument('files',

metavar='image',

nargs='+',

help='Images

you

wish

to

test')

parser.add_argument('-r',

'--resize',

action='store_true',

help='Reduce

image

size

to

increase

speed

of

scanning')

parser.add_argument('-v',

'--visualization',

action='store_true',

help='Generating

areas

of

skin

image')

args

=

parser.parse

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論