我正在尝试使运动检测器工作。香港专业教育学院使用了一些来自英特尔的代码,香港专业教育学院使它与我的网络摄像头一起工作。但我希望将运动检测功能显示在我的屏幕上,而不是我的网络摄像头上。香港专业教育学院让ImageGrab工作,我什至可以使用cv2转换图像流。但是我似乎无法找到一种方法来使ImageGrab和VideoCapture协同工作
import numpy as np
from PIL import ImageGrab
import cv2
def draw_lines(img, lines):
try:
for line in lines:
coords = line[0]
cv2.line(img, (coords[0],coords[1]), (coords[2],coords[3]),
[255,255,255], 3)
except:
pass
def process_img(original_image):
processed_img = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
processed_img = cv2.Canny(processed_img, threshold1=100, threshold2=300)
processed_img = cv2.GaussianBlur(processed_img, (3,3), 0)
lines = cv2.HoughLinesP(processed_img, 1, np.pi/180, 180, 10, 15)
draw_lines(processed_img,lines)
return processed_img
screen = np.array(ImageGrab.grab(bbox=(620,150,1300,620)))
new_screen = process_img(screen)
sdThresh = 15
font = cv2.FONT_HERSHEY_SIMPLEX
#TODO: Face Detection 1
def distMap(frame1, frame2):
"""outputs pythagorean distance between two frames"""
frame1_32 = np.float32(frame1)
frame2_32 = np.float32(frame2)
diff32 = frame1_32 - frame2_32
norm32 = np.sqrt(diff32[:,:,0]**2 + diff32[:,:,1]**2 +
diff32[:,:,2]**2)/np.sqrt(255**2 + 255**2 + 255**2)
dist = np.uint8(norm32*255)
return dist
cv2.namedWindow('frame')
cv2.namedWindow('dist')
#capture video stream from camera source. 0 refers to first camera, 1 referes to 2nd and so on.
#cap = cv2.VideoCapture(0)
cap = cv2.VideoCapture(0)
_, frame1 = cap.read()
_, frame2 = cap.read()