所以我有几个程序从去年的opencv,所以我只是想尝试他们,但是,opencv检测远程相机,如DroidCam,但不能检测到我的usb连接的相机,这工作得很好与相机应用程序在Windows上。我试着输入usb id,但那不起作用。我试着重新启动我的笔记本电脑,认为这是笔记本电脑的问题,但这也不起作用。所以我意识到它不能检测相机id 0和1。我也不知道为什么,因为我记得去年我的相机在id# 0上工作得很好。确切地说,这个节目从2020年12月14日开始。下面是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
注意:我确实遵循了一个教程,并不是说这段代码是我的。
使用
ret, cam = cap.read()
一定要检查ret
(或至少测试cam is not None:
)一些相机需要几帧来"预热"。