我们目前正在整合两个代码: 1. mss图像抓取屏幕录制 2. opencv颜色跟踪器
我们遇到了这个错误:
File "C:/Users/John Wong/Desktop/Test2.py", line 28, in <module>
_, img = cap.read()
AttributeError: 'NoneType' object has no attribute 'read'
我们希望使用屏幕抓取作为颜色跟踪代码的输入。会发生的是,当屏幕捕获代码正在进行时,颜色跟踪代码将同时在捕获的屏幕上生效。
我们不知道我们的代码有什么问题,以下是以下脚本:
import cv2
import numpy as np
import time
import mss
import numpy
with mss.mss() as sct:
# Part of the screen to capture
monitor = {'top': 80, 'left': 20, 'width': 800, 'height': 770}
while 'Screen capturing':
last_time = time.time()
# Get raw pixels from the screen, save it to a Numpy array
img = numpy.array(sct.grab(monitor))
cap = cv2.imshow('OpenCV/Numpy normal', img)
while(True):
_, img = cap.read()
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
red_lower = np.array([170,87,97],np.uint8)
red_upper = np.array([180,255,255],np.uint8)
blue_lower = np.array([23,59,119],np.uint8)
blue_upper = np.array([54,255,255],np.uint8)
yellow_lower = np.array([0,50,80],np.uint8)
yellow_upper = np.array([20,255,255],np.uint8)
red = cv2.inRange(hsv, red_lower, red_upper)
blue = cv2.inRange(hsv, blue_lower, blue_upper)
yellow = cv2.inRange(hsv, yellow_lower, yellow_upper)
kernal = np.ones((5,5), "uint8")
red = cv2.dilate(red,kernal)
res = cv2.bitwise_and(img, img, mask = red)
blue = cv2.dilate(blue, kernal)
res1 = cv2.bitwise_and(img, img, mask = blue)
yellow = cv2.dilate(yellow, kernal)
res2 = cv2.bitwise_and(img, img, mask = yellow)
(_,contours, heirarchy) = cv2.findContours(red,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area>300):
x,y,w,h = cv2.boundingRect(contour)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(61,26,76),2)
cv2.putText(img,"PRIORITY",(x,y),cv2.FONT_HERSHEY_SIMPLEX,0.7,(61,26,76))
(_,contours, heirarchy) = cv2.findContours(blue,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area>300):
x,y,w,h = cv2.boundingRect(contour)
img = cv2.rectangle(img,(x,y),(x+w,y+h), (255,0,0),2)
cv2.putText(img,"SECOND",(x,y),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,0,0))
(_,contours,heirarchy) = cv2.findContours(yellow,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area>300):
x,y,w,h = cv2.boundingRect(contour)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,217),2)
cv2.putText(img,"ALERT",(x,y),cv2.FONT_HERSHEY_SIMPLEX,1.0, (0,255,217))
cv2.imshow("Color Tracking", img)
if cv2.waitKey(10) & 0xFF == ord('q'):
cap.release()
cv2.destroyAllWindows()
break
提前谢谢! :)
答案 0 :(得分:1)
我真的不明白这段代码中发生了什么,以及你期望cap
是什么,但是:
这一行将图像读入img
img = numpy.array(sct.grab(monitor))
然后使用以下行在窗口中显示该图像:
cap = cv2.imshow('OpenCV/Numpy normal', img)
cap
现在是NoneType
,如您的错误消息所示(感谢@DanMašek),因此,不要使用Cap作为其余代码中的图像,而是使用img代替。
while(True):
# Remove this line: _, img = cap.read()
# Get raw pixels from the screen, save it to a Numpy array
img = numpy.array(sct.grab(monitor)) #Update the new image
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
red_lower = np.array([170,87,97],np.uint8)
red_upper = np.array([180,255,255],np.uint8)
blue_lower = np.array([23,59,119],np.uint8)
blue_upper = np.array([54,255,255],np.uint8)
yellow_lower = np.array([0,50,80],np.uint8)
yellow_upper = np.array([20,255,255],np.uint8)
red = cv2.inRange(hsv, red_lower, red_upper)
blue = cv2.inRange(hsv, blue_lower, blue_upper)
yellow = cv2.inRange(hsv, yellow_lower, yellow_upper)
cv2.imshow('Processed', img)
cv2.waitKey(1)