所以我去年有几个用于 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
注意:我确实遵循了一个教程,并没有声称这段代码是我的。