所以我有一对夫妇的程序从去年的opencv所以我只是想尝试他们,但是,Opencv检测远程摄像头一样,DroidCam,但不能检测我的usb连接的相机,它的工作就好与相机应用程序在Windows上。我试图输入usb id,但它不工作。我试图重新启动我的笔记本电脑以为这是一个笔记本电脑的问题,但它不工作,所以我意识到它不能检测摄像头id 0和1。我不知道为什么,因为我记得去年我的摄像头在id# 0下工作得很好。这个程序是从12/14/2020开始的。下面是py中的代码:
import cv2
#############################################
frameWidth = 1500
frameHeight = 480
frontalFaceCascade = cv2.CascadeClassifier("file_location_and_name.xml")
FullBody = cv2.CascadeClassifier("file_location_and_name.xml")
minArea = 200
color = (255, 0, 255)
colour = (0, 255, 255)
colour2 = (0, 0, 255)
###############################################
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10, 150)
count = 0
running = True
while running:
ret, cam = cap.read()
imgGray = cv2.cvtColor(cam, cv2.COLOR_BGR2GRAY)
face = frontalFaceCascade.detectMultiScale(imgGray, 1.1, 10, cv2.CASCADE_FIND_BIGGEST_OBJECT)
boy = FullBody.detectMultiScale(imgGray, 1.1, 10, cv2.CASCADE_DO_ROUGH_SEARCH)
for (x, y, w, h) in face:
area = w*h
if area > minArea:
cv2.rectangle(cam, (x, y), (x + w, y + h), (255, 0, 255), 2)
cv2.putText(cam, "Face", (x, y - 5), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, color, 2)
imgRoi = cam[y:y + h, x:x + w]
cv2.imshow("Result", cam)
if cv2.waitKey(1) and 0xFF == ord('s'):
cv2.imwrite("Resources/Scanned/NoPlate_"+str(count)+".jpg", imgRoi)
cv2.rectangle(cam, (0, 200), (640, 300), (0, 255, 0), cv2.FILLED)
cv2.putText(cam, "Scan Saved", (150, 265), cv2.FONT_HERSHEY_DUPLEX, 2, (0, 0, 255), 2)
cv2.imshow("Result", cam)
cv2.waitKey(500)
count += 1
for (x, y, w, h) in boy:
area = w*h
if area > minArea:
cv2.rectangle(cam, (x, y), (x + w, y + h), colour2, 2)
cv2.putText(cam, "No mask", (x, y - 5), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, colour2, 2)
imgRoi = cam[y:y + h, x:x + w]
cv2.imshow("Result", cam)
if cv2.waitKey(1) and 0xFF == ord('s'):
cv2.imwrite("Resources/Scanned/NoPlate_"+str(count)+".jpg", imgRoi)
cv2.rectangle(cam, (0, 200), (640, 300), (0, 255, 0), cv2.FILLED)
cv2.putText(cam, "Scan Saved", (150, 265), cv2.FONT_HERSHEY_DUPLEX, 2, (0, 0, 255), 2)
cv2.imshow("Result", cam)
cv2.waitKey(500)
count += 1
注意:我确实遵循了教程,并不声称这段代码是我的。
1条答案
按热度按时间tzdcorbm1#
当您使用
一定要检查
ret
(或至少测试cam is not None:
)有些相机需要几帧来"预热"。