2

So I had a couple of programs from last year for opencv so I just wanted to try them but, Opencv detects remote cameras like, DroidCam but can't detect my usb connected camera which works just fine with the camera app on Windows. I tried entering the usb id but that doesn't work. I tried restarting my laptop thinking it is a laptop problem but that doesn't work, either. So I realized it can't detect the camera id 0 and 1. Which I don't know why cause I remember my camera working fine with id# 0 last year. This programs are from 12/14/2020 to be exact. Here's the code in 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

NOTE: I did follow a tutorial and am not claiming this code is mine.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
QanBan
  • 47
  • 1
  • 8
  • For what it's worth, -1072873851 is 0xc00d3e85, which is MF_E_SHUTDOWN. You're sure your camera supports 1500x480? That's a very unusual format for a basic web camera. – Tim Roberts Sep 05 '21 at 03:47
  • Oh that's because I was messing around with it to see if it changes anything. It doesn't – QanBan Sep 05 '21 at 03:53

1 Answers1

0

When you use

ret, cam = cap.read()

be sure to check ret (or least test that cam is not None:) Some cameras take a few frames to 'warm up'.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46