简体   繁体   中英

Controlling USB Thorlabs camera via Python - OpenCV

There are several topics on this but many of them are very very old and no real solutions have been offered (or none that work for me for sure).

I am trying various libraries to get Python to read the frames of my USB camera (DCC1545M) and they all have various module or DLL import errors. I'm trying Instrumental, Thorcam API, py-harware, micromanager..

Specifically I would ideally love to get it to work with OpenCV, because of all the useful computer vision features that you can later use on the image, which I am not sure you can do with the other libraries.

However, I encounter the same issue as everyone else, in that openCV can not read the USB camera in the first place.

cap = cv2.VideoCapture(1)              ## tried difference indices
  
cap.isOpened()                        ## returns FALSE
img_counter = 0
   
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

while True: 
    ret,frame = cap.read()                     ## returned frame is empty 
    cv2.imshow('preview',frame)
    k = cv2.waitKey(1)
    if k%256==32: # if SPACE is pressed, take image 
        img_name = 'frame_number_{}.png'.format(img_counter)
        cv2.imwrite(img_name,frame)
        print('frame taken ')
        img_counter += 1

cap.release()
cv2.destroyAllWindows()

I have installed the driver from Thorlabs website and I have the uc480_64.dll. The camera is successfully located using the Instrumental() library:

from instrumental import list_instruments, instrument
from ctypes import *

paramsets = list_instruments() ## camera found
print(paramsets)

which returns

[<ParamSet[UC480_Camera] serial=b'4102675270' model=b'C1285R12M' id=1>]

I was wondering if anyone knows if in the last couple of years openCV has managed to find a way to read USB cameras and if so, what is the way? Or of any other reliable method, which allows further image processing on the captured frames.

PS: I posted this on superuser because apparently hardware questions are not allowed on stackoverflow, but suoeruser migrated it back here .. So apologies if it is off-topic here as well.

Can you communicate with the camera it its native software? https://www.thorlabs.com/software_pages/ViewSoftwarePage.cfm?Code=ThorCam

Our lab is using "pylablib cam-control" to communicate with a variety of cameras (including Thorlabs USB ones): https://pylablib-cam-control.readthedocs.io/en/latest/

Or if you would prefer writing your own code, pylablib includes a class for Thorlabs USB cameras (actually has been tested with your specific camera). https://pylablib.readthedocs.io/en/latest/devices/uc480.html#cameras-uc480

Try the following code. It works with my Thorlab DCx camera:

import cv2
import numpy as np
from instrumental.drivers.cameras import uc480

# init camera
instruments = uc480.list_instruments()
cam = uc480.UC480_Camera(instruments[0])

# params
cam.start_live_video(framerate = "10Hz")

while cam.is_open:
     
     frame = cam.grab_image(timeout='100s', copy=True, exposure_time='10ms')
     
     frame1 = np.stack((frame,) * 3,-1) #make frame as 1 channel image
     frame1 = frame1.astype(np.uint8)

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

     #now u can apply opencv features

     cv2.imshow('Camera', gray)
     
     if cv2.waitKey(30) & 0xFF == ord('q'):
        break

cam.close()
cv2.destroyAllWindows()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM