normyx / MMM-Facial-Recognition-OCV3

MIT License
51 stars 18 forks source link

USB Webcam Problem #4

Open jimjay203 opened 6 years ago

jimjay203 commented 6 years ago

I have a ps3 eye usb cam installed. When starting "python tools.capture.py" for capture training i get:

Loading PiCamera PiCamera loaded Capturing positive training images. Press enter to capture an image. Press Ctrl-C to quit. mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:ENOMEM) mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1) Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner self.run() File "/home/pi/MagicMirror/modules/MMM-Facial-Recognition-OCV3/lib/common/picam.py", line 25, in run with picamera.PiCamera() as camera: File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 431, in init self._init_camera(camera_num, stereo_mode, stereo_decimate) File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 460, in _init_camera "Camera is not enabled. Try running 'sudo raspi-config' " PiCameraError: Camera is not enabled. Try running 'sudo raspi-config' and ensure that the camera has been enabled.

What do I have to configure, to get it working with an usb webcam?

normyx commented 6 years ago

Hi No clue. Are you sure webcam is enable and working for your raspi?

diverdennis commented 6 years ago

Hi,

i have the same prob:

`pi@magicmirror:~/MagicMirror/modules/MMM-Facial-Recognition-OCV3 $ python tools.facerecognition.py Remember to set the name list environment variable FACE_USERS Loading PiCamera PiCamera loaded Loading training data... mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:ENOMEM) mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1) Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner self.run() File "/home/pi/MagicMirror/modules/MMM-Facial-Recognition-OCV3/lib/common/picam.py", line 25, in run with picamera.PiCamera() as camera: File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 431, in init self._init_camera(camera_num, stereo_mode, stereo_decimate) File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 460, in _init_camera "Camera is not enabled. Try running 'sudo raspi-config' " PiCameraError: Camera is not enabled. Try running 'sudo raspi-config' and ensure that the camera has been enabled.

Training data loaded! OpenCV Error: Assertion failed (!buf.empty() && buf.isContinuous()) in imdecode, file /home/pi/opencv/modules/imgcodecs/src/loadsave.cpp, line 637 Traceback (most recent call last): File "tools.facerecognition.py", line 45, in frame = camera.read() File "/home/pi/MagicMirror/modules/MMM-Facial-Recognition-OCV3/lib/common/picam.py", line 55, in read image = cv2.imdecode(data, 1) cv2.error: /home/pi/opencv/modules/imgcodecs/src/loadsave.cpp:637: error: (-215) !buf.empty() && buf.isContinuous() in function imdecode ` I don´t have a picam... i am using a webcam which is working when i try with fswebcam.

how to setup the webcam?

diverdennis commented 6 years ago

i tired this script and it works with it... no idea why there is a prob with MMM-Facial-Recognition-OCV3

import cv2

cam = cv2.VideoCapture(0)

cv2.namedWindow("test")

img_counter = 0

while True: ret, frame = cam.read() cv2.imshow("test", frame) if not ret: break k = cv2.waitKey(1)

if k%256 == 27:
    # ESC pressed
    print("Escape hit, closing...")
    break
elif k%256 == 32:
    # SPACE pressed
    img_name = "opencv_frame_{}.png".format(img_counter)
    cv2.imwrite(img_name, frame)
    print("{} written!".format(img_name))
    img_counter += 1

cam.release()

cv2.destroyAllWindows()

diverdennis commented 6 years ago

so when i remove picam.py it uses webcam.py put the i get the error corrupt jpeg data...

Anybody with an idea about this...

MuhammadJamee commented 6 years ago

so when i remove picam.py it uses webcam.py put the i get the error corrupt jpeg data...

Anybody with an idea about this...

Its ok nothing to worry about, i m having same thing

RBirkeland commented 6 years ago

I fixed this problem by editing the picam file located at: /MMM-Facial-Recognition-OCV3/lib/common/picam.py Definitely a quick hack, but works quite OK.

class OpenCVCapture(Thread):
    def __init__(self, preview=False):
        Thread.__init__(self)
        self.buffer = io.BytesIO()
        self.lock = threading.Lock()
        self.running = True
        self.preview = preview
    self.cap = None

    def run(self):
    self.cap = cv2.VideoCapture(0)
    if (self.cap.isOpened()== False): 
        print("Error opening video stream or file")
    self.lock.release()

    def read(self):
        """Read a single frame from the camera and return the data as an OpenCV
        image (which is a numpy array).
        """
        self.lock.acquire()
        try:
        ret, frame = self.cap.read()
        print("Took image")
        finally:
            self.lock.release()
        return frame

    def stop(self):
        self.running = False
        self.join()
Axachi commented 5 years ago

Please higlight what you changed. And please make a fix dev. This worked with the original Face rec. And now All I get is corrupted.

Axachi commented 5 years ago

I edited the lib/common/config.js and changed picam to webcam. My usb cam now lights up. This is the fix I used on the old FR. But it still comes with corrupt error.. And it never stops....

LadnerJamie commented 5 years ago

I didn't have any luck with the fixes mentioned previously, but I found that replacing the picam.py file with webcam.py did allow me to run the capture and training tools.

TKNH commented 5 years ago

Hello fellas. Im using a USB Webcam aswell and Ive the same problem like @diverdennis and @jimjay203. The module always wants to use the PiCam to capture the pictures. i tried to delete the picam.py, it works then and the usbcamera is active but its capturing a loop of corrupt pictures. in the config.js i changed the useUSBCam part to true too. do you know any fixes?

ipchelnikov commented 5 years ago

Hi, all, I also had the same problem. Both tools.capture.py and tools.facerecognition.py returned the following error on attempt to access the USB camera: "Camera is not enabled. Try running 'sudo raspi-config' "

And the config changes did not help in any way. Then I found the problematic place in ToolsConfig (/lib/tools/config.py). As I understand the following logic, it supposes to switch to USB Cam if PI Cam returns an error, however it does not happen for some reason.

I am not an expert in Python, so I do not quite sure how to fix it properly. As a workaround I just replaced import picam with import webcam in the try section and it helped:

diff --git a/lib/tools/config.py b/lib/tools/config.py
index 6087482..078b6df 100755
--- a/lib/tools/config.py
+++ b/lib/tools/config.py
@@ -83,16 +83,19 @@ class ToolsConfig (CommonConfig):
     @classmethod
     def getCamera(cls):
         try:
-            import picam
-            print("Loading PiCamera")
-            capture = picam.OpenCVCapture(True)
-            print("PiCamera loaded")
-            capture.start()
-            return capture
-        except Exception as e:
-            print(e)
+            #import picam
+            #print("Loading PiCamera")
+            #capture = picam.OpenCVCapture(True)
+            #print("PiCamera loaded")
+            #capture.start()
+            #return capture
+            print("!!! Loading WebCamera")
             import webcam
             return webcam.OpenCVCapture(device_id=0)
+        except Exception as e:
+            print(e)
+            #import webcam
+            #return webcam.OpenCVCapture(device_id=0)

     @classmethod
     def model(cls):

I guess it requires a change request for the future..

kemalcam commented 5 years ago

i run the facerecognition.py. it will work but. i doesnt recognize me. it return none. i didnt find to solve this problem. What can i do?