336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import time
import cv2
import numpy as np
from mss import mss

red_color = (255, 255, 255)

def checkImage(src, dst):
difference = cv2.subtract(src, dst)
ret, difference = cv2.threshold(difference, 55, 255, cv2.THRESH_BINARY)
nzCount = cv2.countNonZero(difference)
return nzCount

def checkContentsImage(src, srcNum, imageContents1 , imageContents2, imageContents3, imageContents4, lcapturedImage ):


returnValue = 0
if checkImage(src, imageContents1) < imageContentsTh:
print("Detected " + str(srcNum) + "-1 " + str(checkImage(src, imageContents1)))
returnValue = 1
if checkImage(src, imageContents2) < imageContentsTh:
print("Detected " + str(srcNum) + "-2 " + str(checkImage(src, imageContents2)))
returnValue = 2
if checkImage(src, imageContents3) < imageContentsTh:
print("Detected " + str(srcNum) + "-3 " + str(checkImage(src, imageContents3)))
returnValue = 3
if checkImage(src, imageContents4) < imageContentsTh:
print("Detected " + str(srcNum) + "-4 " + str(checkImage(src, imageContents4)))
returnValue = 4

if returnValue != 0:
if srcNum == 1:
lcapturedImage = cv2.rectangle(lcapturedImage, (3, 1), (116, 115), red_color, 2)
if srcNum == 3:
lcapturedImage = cv2.rectangle(lcapturedImage, (3, 1+141), (116, 115+141), red_color, 2)
if srcNum == 5:
lcapturedImage = cv2.rectangle(lcapturedImage, (3, 1+141+141), (116, 115+141+141), red_color, 2)
if srcNum == 7:
lcapturedImage = cv2.rectangle(lcapturedImage, (3, 1+141+141+141), (116, 115+141+141+141), red_color, 2)

if srcNum == 2:
lcapturedImage = cv2.rectangle(lcapturedImage, (143, 1), (143+116, 115), red_color, 2)
if srcNum == 4:
lcapturedImage = cv2.rectangle(lcapturedImage, (143, 1+ 141), (143+116, 115+141), red_color, 2)
if srcNum == 6:
lcapturedImage = cv2.rectangle(lcapturedImage, (143, 1 + 141 + 141), (143+116, 115+141+141), red_color, 2)
if srcNum == 8:
lcapturedImage = cv2.rectangle(lcapturedImage, (143, 1 + 141 + 141 + 141), (143+116, 115+141+141+141), red_color, 2)

return returnValue



print("load finger png files...")
imageFinger1 = cv2.imread("finger1.bmp", cv2.IMREAD_GRAYSCALE)
imageFinger2 = cv2.imread("finger2.bmp", cv2.IMREAD_GRAYSCALE)
imageFinger3 = cv2.imread("finger3.bmp", cv2.IMREAD_GRAYSCALE)
imageFinger4 = cv2.imread("finger4.bmp", cv2.IMREAD_GRAYSCALE)
#imageFinger1 = cv2.imread("finger1.bmp")
#imageFinger2 = cv2.imread("finger2.bmp")
#imageFinger3 = cv2.imread("finger1.png")
#imageFinger4 = cv2.imread("finger1.png")
print("load finger png files... Done")


with mss() as sct:
mon1 = sct.monitors[1]
mon2 = sct.monitors[2]
box = {'top': mon1['top'] +178, 'left': mon1['left'] + 913, 'width': 458, 'height': 516}
componentsbox = {'top': mon1['top'] +290, 'left': mon1['left'] + 486, 'width': 257, 'height': 536}
componentsbox1 = {'top': mon1['top'] +290, 'left': mon1['left'] + 486, 'width': 114, 'height': 115}
componentsbox2 = {'top': mon1['top'] +290, 'left': mon1['left'] + 626, 'width': 114, 'height': 115}
componentsbox3 = {'top': mon1['top'] +430, 'left': mon1['left'] + 486, 'width': 114, 'height': 115}
componentsbox4 = {'top': mon1['top'] +430, 'left': mon1['left'] + 626, 'width': 114, 'height': 115}
componentsbox5 = {'top': mon1['top'] +570, 'left': mon1['left'] + 486, 'width': 114, 'height': 115}
componentsbox6 = {'top': mon1['top'] +570, 'left': mon1['left'] + 626, 'width': 114, 'height': 115}
componentsbox7 = {'top': mon1['top'] +710, 'left': mon1['left'] + 486, 'width': 114, 'height': 115}
componentsbox8 = {'top': mon1['top'] +710, 'left': mon1['left'] + 626, 'width': 114, 'height': 115}

DebugMode = False
# mon = sct.monitors[0]
while True:
last_time = time.time()
capturedImage = np.array(sct.grab(box))
capturedImage = cv2.cvtColor(capturedImage, cv2.COLOR_BGR2GRAY)

#ret, capturedImage = cv2.threshold(capturedImage, 127, 255, cv2.THRESH_BINARY)
#ret, imageFinger1 = cv2.threshold(imageFinger1, 127, 255, cv2.THRESH_BINARY)
#ret, imageFinger2 = cv2.threshold(imageFinger2, 127, 255, cv2.THRESH_BINARY)

#capturedImage = cv2.cvtColor(capturedImage, cv2.COLOR_BGR2RGB)
#capturedImage = cv2.cvtColor(capturedImage, cv2.COLOR_BGR2GRAY)
#capturedImage = cv2.threshold(capturedImage, 127,255, cv2.THRESH_BINARY)
#imageFinger1 = cv2.threshold(imageFinger1, 127, 255, cv2.THRESH_BINARY)
#imageFinger2 = cv2.threshold(imageFinger2, 127, 255, cv2.THRESH_BINARY)


#print('fps: {0}'.format(1 / (time.time()-last_time)))

#cv2.imshow('capturedImage', capturedImage)

difference = cv2.subtract(capturedImage, imageFinger1)
ret, difference = cv2.threshold(difference, 55, 255, cv2.THRESH_BINARY)
if DebugMode == True:
cv2.imshow('imageFinger1', difference)
nzCount1 = cv2.countNonZero(difference)
#print("imageFinger1 : " + str(nzCount1))

difference = cv2.subtract(capturedImage, imageFinger2)
ret, difference = cv2.threshold(difference, 55, 255, cv2.THRESH_BINARY)
if DebugMode == True:
cv2.imshow('imageFinger2', difference)
nzCount2 = cv2.countNonZero(difference)
#print("imageFinger2 : " + str(nzCount2))



difference = cv2.subtract(capturedImage, imageFinger3)
ret, difference = cv2.threshold(difference, 55, 255, cv2.THRESH_BINARY)
if DebugMode == True:
cv2.imshow('imageFinger3', difference)
nzCount3 = cv2.countNonZero(difference)
#print("imageFinger3 : " + str(nzCount3))

difference = cv2.subtract(capturedImage, imageFinger4)
ret, difference = cv2.threshold(difference, 55, 255, cv2.THRESH_BINARY)
if DebugMode == True:
cv2.imshow('imageFinger4', difference)
nzCount4 = cv2.countNonZero(difference)
#print("imageFinger4 : " + str(nzCount4))
imageFingerNumber = 0

if nzCount1 < 10000 and nzCount2 > 10000 and nzCount3 > 10000 and nzCount4 > 10000:
print("imageFinger1 : " + str(nzCount1))
imageFingerNumber = 1

if nzCount1 > 10000 and nzCount2 < 10000 and nzCount3 > 10000 and nzCount4 > 10000:
print("imageFinger2 : " + str(nzCount2))
imageFingerNumber = 2

if nzCount1 > 10000 and nzCount2 > 10000 and nzCount3 < 10000 and nzCount4 > 10000:
print("imageFinger3 : " + str(nzCount3))
imageFingerNumber = 3

if nzCount1 > 10000 and nzCount2 > 10000 and nzCount3 > 10000 and nzCount4 < 10000:
print("imageFinger4 : " + str(nzCount4))
imageFingerNumber = 4



capturedImage = np.array(sct.grab(componentsbox))
capturedImage = cv2.cvtColor(capturedImage, cv2.COLOR_BGR2GRAY)
cv2.imshow('ContentsBox', capturedImage)

if imageFingerNumber != 0:
print("Detected finger Number : !!!" + str(imageFingerNumber))

imageContents1 = cv2.imread("Acontent"+ str(imageFingerNumber) + "-1.bmp", cv2.IMREAD_GRAYSCALE)
imageContents2 = cv2.imread("Acontent"+ str(imageFingerNumber) + "-2.bmp", cv2.IMREAD_GRAYSCALE)
imageContents3 = cv2.imread("Acontent"+ str(imageFingerNumber) + "-3.bmp", cv2.IMREAD_GRAYSCALE)
imageContents4 = cv2.imread("Acontent"+ str(imageFingerNumber) + "-4.bmp", cv2.IMREAD_GRAYSCALE)

imageContentsTh = 120

#cv2.imshow('ContentsBox', capturedImage )

capturedImage1 = np.array(sct.grab(componentsbox1))
capturedImage1 = cv2.cvtColor(capturedImage1, cv2.COLOR_BGR2GRAY)

checkContentsImage(capturedImage1, 1, imageContents1, imageContents2, imageContents3, imageContents4, capturedImage)

capturedImage2 = np.array(sct.grab(componentsbox2))
capturedImage2 = cv2.cvtColor(capturedImage2, cv2.COLOR_BGR2GRAY)

checkContentsImage(capturedImage2, 2, imageContents1, imageContents2, imageContents3, imageContents4, capturedImage)

capturedImage3 = np.array(sct.grab(componentsbox3))
capturedImage3 = cv2.cvtColor(capturedImage3, cv2.COLOR_BGR2GRAY)

checkContentsImage(capturedImage3, 3, imageContents1, imageContents2, imageContents3, imageContents4, capturedImage)

capturedImage4 = np.array(sct.grab(componentsbox4))
capturedImage4 = cv2.cvtColor(capturedImage4, cv2.COLOR_BGR2GRAY)


checkContentsImage(capturedImage4, 4, imageContents1, imageContents2, imageContents3, imageContents4, capturedImage)

capturedImage5 = np.array(sct.grab(componentsbox5))
capturedImage5 = cv2.cvtColor(capturedImage5, cv2.COLOR_BGR2GRAY)



checkContentsImage(capturedImage5, 5, imageContents1, imageContents2, imageContents3, imageContents4, capturedImage)

capturedImage6 = np.array(sct.grab(componentsbox6))
capturedImage6 = cv2.cvtColor(capturedImage6, cv2.COLOR_BGR2GRAY)


checkContentsImage(capturedImage6, 6, imageContents1, imageContents2, imageContents3, imageContents4, capturedImage)

capturedImage7 = np.array(sct.grab(componentsbox7))
capturedImage7 = cv2.cvtColor(capturedImage7, cv2.COLOR_BGR2GRAY)


checkContentsImage(capturedImage7, 7, imageContents1, imageContents2, imageContents3, imageContents4, capturedImage)


capturedImage8 = np.array(sct.grab(componentsbox8))
capturedImage8 = cv2.cvtColor(capturedImage8, cv2.COLOR_BGR2GRAY)


checkContentsImage(capturedImage8, 8, imageContents1, imageContents2, imageContents3, imageContents4, capturedImage)


#Check Contents1
cv2.imshow('ContentsBox', capturedImage)

if cv2.waitKey(1000) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break

'Programming > Python' 카테고리의 다른 글

[Python/OpenCV] Near-Duplicate Image Detection #1  (0) 2020.06.02
[Python] List 정렬 프로그램 구현  (0) 2017.02.06
[Python 3.6] * 찍기  (0) 2017.02.02