5 Replies Latest reply on May 10, 2018 12:44 PM by MarioTavarez

    USB CAMERA WITH OPENCV

    MarioTavarez

      how can i see from a usb camera with opencv?

      My usb camera is /dev/video14

      and I would like to see from the usb camera with the following code:

       

      import numpy as np

      import cv2

       

      cap = cv2.VideoCapture(0)

       

      while(True):

          # Capture frame-by-frame

          ret, frame = cap.read()

       

          # Our operations on the frame come here

          gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

       

          # Display the resulting frame

          cv2.imshow('frame',gray)

          if cv2.waitKey(1) & 0xFF == ord('q'):

              break

       

      # When everything done, release the capture

      cap.release()

      cv2.destroyAllWindows()

       

       

      I know that with the r200 this code is used

       

      import logging

      logging.basicConfig(level=logging.INFO)

       

      import time

      import numpy as np

      import cv2

      import pyrealsense as pyrs

      from pyrealsense.constants import rs_option

       

       

      with pyrs.Service() as serv:

          with serv.Device() as dev:

       

              dev.apply_ivcam_preset(0)

       

              try:  # set custom gain/exposure values to obtain good depth image

                  custom_options = [(rs_option.RS_OPTION_R200_LR_EXPOSURE, 30.0),

                                    (rs_option.RS_OPTION_R200_LR_GAIN, 100.0)]

                  dev.set_device_options(*zip(*custom_options))

              except pyrs.RealsenseError:

                  pass  # options are not available on all devices

       

              cnt = 0

              last = time.time()

              smoothing = 0.9

              fps_smooth = 30

       

              while True:

       

                  cnt += 1

                  if (cnt % 10) == 0:

                      now = time.time()

                      dt = now - last

                      fps = 10/dt

                      fps_smooth = (fps_smooth * smoothing) + (fps * (1.0-smoothing))

                      last = now

       

                  dev.wait_for_frames()

                  c = dev.color

                  c = cv2.cvtColor(c, cv2.COLOR_RGB2BGR)

                  d = dev.depth * dev.depth_scale * 1000

                  d = cv2.applyColorMap(d.astype(np.uint8), cv2.COLORMAP_RAINBOW)

       

                  cd = np.concatenate((c, d), axis=1)

       

                  cv2.putText(cd, str(fps_smooth)[:4], (0, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 0))

       

                  cv2.imshow('', cd)

                  if cv2.waitKey(1) & 0xFF == ord('q'):

                     break

       

      but I throw this error when I run the code, I guess the error comes out because it is not the r200, how could my camera usb work, by the way I've already tried it and if it captures photos

       

        • 1. Re: USB CAMERA WITH OPENCV
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          Hi MarioTavarez, 

          Thank you for your interest in the Intel Aero drone. 
          Please let me look into it and I will get back to you later. 

          Regards,
          Alexandra

          • 2. Re: USB CAMERA WITH OPENCV
            Intel Corporation
            This message was posted on behalf of Intel Corporation

            Hello MarioTavarez,

            As stated in the package description, MacOS and Linux packages do not support video related functionality (not compiled with FFmpeg). You will have to compile OpenCV manually.
            Let me know if you have further questions.

            Regards,
            Alexandra

            • 3. Re: USB CAMERA WITH OPENCV
              MarioTavarez

              I have already compiled opencv before including opencvcontrib that contains its extra modules that I use and I need to use in the intel aero but I mark that error when I run that little script on the camera, I do not know if I take it as the main camera, I just need to Call usb camera through an opencv videocapture function but it does not work

              • 4. Re: USB CAMERA WITH OPENCV
                Intel Corporation
                This message was posted on behalf of Intel Corporation

                Hi MarioTavarez,

                I am still investigating your issue. 
                Could you tell me what camera model do you have?

                Regards,
                Alexandra

                • 5. Re: USB CAMERA WITH OPENCV
                  MarioTavarez

                  I found the solution to the problem, in the videocapture () function you have to pass the path of the device in this case I want to use the 14 ie the usb camera, therefore I put this code in case you are interested in documenting

                   

                  import numpy as np

                  import cv2

                   

                  cap = cv2.VideoCapture('/dev/video14') #In this part I put the route camera

                   

                  while(True):

                      # Capture frame-by-frame

                      ret, frame = cap.read()

                   

                      # Our operations on the frame come here

                      gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

                   

                      # Display the resulting frame

                      cv2.imshow('frame',gray)

                      if cv2.waitKey(1) & 0xFF == ord('q'):

                          break

                   

                  # When everything done, release the capture

                  cap.release()

                  cv2.destroyAllWindows()