I am facing issue while accessing USB camera using beagle-bone black wireless.
Firstly the error is "select timeout" exception which was resolved by this post
Now I am facing the black screen in output.
Here is the testing code I am using.
from cv2 import *
# initialize the camera
cam = VideoCapture(0) # 0 -> index of camera
print "Cam capture"
cam.set(3,320)
cam.set(4,240)
print "Cam set"
s, img = cam.read()
print "Cam read"
if s: # frame captured without any errors
namedWindow("cam-test",CV_WINDOW_AUTOSIZE)
imshow("cam-test",img)
while True:
key = waitKey(30)
if key == ord('q') :
destroyWindow("cam-test")
I have already check that video0 in /dev directory.
The issue is that you need to call 'cam.read()andimshow()` inside the while loop
What you're doing is that you're reading just the first frame, then showing it, and you whileloop isn't doing anything. When the camera boots, the first frame is just a blank screen , which is what you see.
The code should be more like:
while True:
s, img = cam.read()
imshow("cam-test",img)
key = waitKey(30)
if key == ord('q') :
destroyWindow("cam-test")
Related
I am trying to create a simple trackbar to test some edge detection, and I've been following the official opencv tutorial from here in Python. When I run the code, the window is created and I can see the sliders, but when I click the sliders, the kernel crashes.
I tried this with some other code example from the internet and it crashed then as well. Basically the moment I click in the region of the slider, the kernel crashes. I know that one thing you seem to need to do in macs is increase the waitKey time and I did do that.
How can I make this work?
def canny_threshold(low_val, src, src_gray):
low_threshold = low_val
img_blur = cv.blur(src_gray, (3,3))
detected_edges = cv.Canny(img_blur, low_threshold, low_threshold * RATIO)
mask = detected_edges != 0
dst = src * (mask[:,:, None].astype(src.dtype))
return dst
src = cv.imread("magpie_house.jpeg")
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
def nothing(x):
pass
cv.namedWindow(WINDOW_NAME)
cv.startWindowThread()
cv.createTrackbar(TITLE_TRACK_BAR, WINDOW_NAME, 0, MAX_LOW_THRESHOLD, nothing)
cv.setTrackbarPos(TITLE_TRACK_BAR, WINDOW_NAME, 50)
while True:
COUNTER += 1
if COUNTER >= 700:
break
low_val = cv.getTrackbarPos(TITLE_TRACK_BAR, WINDOW_NAME)
dst = canny_threshold(low_val, src, src_gray)
cv.imshow(WINDOW_NAME, dst)
if cv.waitKey(10) & 0xFF == ord("q"):
break
cv.waitKey(1)
cv.destroyAllWindows()
It appears that there is a known issue on macs regarding trackbars.
I was able to get the current image from a Thorlabs uc480 camera using instrumental. My issue is when I try to adjust the parameters for grab_image. I can change cx and left to any value and get an image. But cy and top only works if cy=600 and top=300. The purpose is to create a GUI so that the user can select values for these parameters to zoom in/out an image.
Here is my code
import instrumental
from instrumental.drivers.cameras import uc480
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
paramsets = instrumental.list_instruments()
cammer = instrumental.instrument(paramsets[0])
plt.figure()
framer= cammer.grab_image(timeout='1s',copy=True,n_frames=1,exposure_time='5ms',cx=640,
left=10,cy=600,top=300)
plt.pcolormesh(framer)
The above code does not give an image if I choose cy=600 and top=10. Are there any particular value set to be used for these parameters? How can I get an image of the full sensor size?
Thorlabs has a Python programming interface available as a download on their website. It is very well documented, and can be installed locally via pip.
Link:
https://www.thorlabs.com/software_pages/ViewSoftwarePage.cfm?Code=ThorCam
Here is an example of a simple capture algorithm that might help get you started:
from thorlabs_tsi_sdk.tl_camera import TLCameraSDK
from thorlabs_tsi_sdk.tl_mono_to_color_processor import MonoToColorProcessorSDK
from thorlabs_tsi_sdk.tl_camera_enums import SENSOR_TYPE
# open the TLCameraSDK dll
with TLCameraSDK() as sdk:
cameras = sdk.discover_available_cameras()
if len(cameras) == 0:
print("Error: no cameras detected!")
with sdk.open_camera(cameras[0]) as camera:
#camera.disarm() # ensure any previous session is closed
# setup the camera for continuous acquisition
camera.frames_per_trigger_zero_for_unlimited = 0
camera.image_poll_timeout_ms = 2000 # 2 second timeout
camera.arm(2)
# need to save the image width and height for color processing
image_width = camera.image_width_pixels
image_height = camera.image_height_pixels
# initialize a mono to color processor if this is a color camera
is_color_camera = (camera.camera_sensor_type == SENSOR_TYPE.BAYER)
mono_to_color_sdk = None
mono_to_color_processor = None
if is_color_camera:
mono_to_color_sdk = MonoToColorProcessorSDK()
mono_to_color_processor = mono_to_color_sdk.create_mono_to_color_processor(
camera.camera_sensor_type,
camera.color_filter_array_phase,
camera.get_color_correction_matrix(),
camera.get_default_white_balance_matrix(),
camera.bit_depth
)
# begin acquisition
camera.issue_software_trigger()
# get the next frame
frame = camera.get_pending_frame_or_null()
# initialize frame attempts and max limit
frame_attempts = 0
max_attempts = 10
# if frame is null, try to get a frame until
# successful or until max_attempts is reached
if frame is None:
while frame is None:
frame = camera.get_pending_frame_or_null()
frame_attempts += 1
if frame_attempts == max_attempts:
raise TimeoutError("Timeout was reached while polling for a frame, program will now exit")
image_data = frame.image_buffer
if is_color_camera:
# transform the raw image data into RGB color data
color_data = mono_to_color_processor.transform_to_24(image_data, image_width, image_height)
save_data = np.reshape(color_data,(image_height, image_width,3))
camera.disarm()
You can also process the image after capture with the PIL library.
I've been working on this project to detect qr-code and make attendance and have the outputs in sql database and so far I've gotten everything to work out,
The Problem is I actually want the frame to close when a single QR code is detected, decoded and all of the above process is completed also The camera works and the frame shows up but it is very slow when detecting the QR code.
What actually happens is after the frame is open it constantly and after it detects a QR code, it actually logs multiple (around 15) instances of a QR being detected after like 2 seconds. Also the frame is still up and I can still detect Images.
The frame closes, only after pressing the waitkey which is 27 or 'Esc'
So I am actually looking for 2 Things:
How to close the frame after detecting a QR Code?
(additional question) how do I detect only one qr code (whether the frame closes or not). So when I scan the QR code, either the frame closes and I am left with one decoded data OR I scan the QR code and the frame remains open, until I hit 'Esc' and I am left with one decoded data.
here is the full code for reference:
import cv2
import os
import csv
import sqlite3
import vobject
from datetime import datetime
import numpy as np
from pyzbar import pyzbar
path = 'qr-codes'
images = []
classNames = []
lists = os.listdir(path)
for cl in lists:
curImage = cv2.imread(f'{path}/{cl}')
images.append(curImage)
classNames.append(os.path.splitext(cl)[0])
def main():
camera = cv2.VideoCapture(0)
ret, frame = camera.read()
while ret:
ret, frame = camera.read()
recognizer = parse_vcard(frame)
win_name = 'Face and QR Detection'
cv2.imshow(win_name, recognizer)
if cv2.waitKey(1) & 0xFF == 27:
break
camera.release()
cv2.destroyAllWindows()
def parse_vcard(frame):
barcodes = pyzbar.decode(frame)
for barcode in barcodes:
x, y, w, h = barcode.rect
cv2.rectangle(frame, (x, y),(x+w, y+h), (0, 255, 0), 2)
mydata = barcode.data.decode('utf-8')
vcard = vobject.readOne(mydata)
make_attendence(vcard)
return frame
def make_attendence(vcard):
name = str(vcard.contents['n'][0].value)
# print(type(Employee.name))
profession = str(vcard.contents['title'][0].value)
now = datetime.now()
date = datetime.date(now)
attendance_time = now.strftime('%H:%M:%S')
leave_time = '21:00:00'
connection = sqlite3.connect('employee.db')
conn = connection.cursor()
# conn.execute("""CREATE TABLE employees (
# name text,
# profission text,
# date date,
# attendance_time time,
# leave_time time
# )""")
conn.execute("INSERT INTO employees VALUES \
(:name, :profession, :date, :attendance_time, :leave_time)", {
'name': name,
'profession': profession,
'date': date,
'attendance_time': attendance_time,
'leave_time': leave_time})
connection.commit()
connection.close()
if __name__ == '__main__':
main()
do not reconnect to the database for every QR code and every picture you take.
connect once. keep the connection.
the imshow window is a window. a "frame" is commonly the term for one picture/image from a video. you can't "close" a frame.
you can close imshow windows with destroyAllWindows or individual imshow windows using destroyWindow
use python's "pdb" debugger. use a profiler to investigate the time cost of your code. use time.perf_counter() and do the measurements yourself, if you don't find a profiler you like.
I'm currently working with a d435 and I want to display IR images (both left and right but for the moments just focus on one), following my code:
import pyrealsense2 as rs
import numpy as np
import cv2
# We want the points object to be persistent so we can display the
#last cloud when a frame drops
points = rs.points()
# Create a pipeline
pipeline = rs.pipeline()
#Create a config and configure the pipeline to stream
config = rs.config()
config.enable_stream(rs.stream.infrared, 1, 1280, 720, rs.format.y8, 30)
# Start streaming
profile = pipeline.start(config)
# Streaming loop
try:
while True:
# Get frameset of color and depth
frames = pipeline.wait_for_frames()
ir1_frame = frames.get_infrared_frame(1) # Left IR Camera, it allows 1, 2 or no input
image = np.asanyarray(ir1_frame)
cv2.namedWindow('IR Example', cv2.WINDOW_AUTOSIZE)
cv2.imshow('IR Example', image)
key = cv2.waitKey(1)
# Press esc or 'q' to close the image window
if key & 0xFF == ord('q') or key == 27:
cv2.destroyAllWindows()
break
finally:
pipeline.stop()
Everything works fine till the line:
cv2.imshow('IR Example', image)
I get the error:
TypeError: mat data type = 17 is not supported
I found this link:
TypeError: src data type = 17 is not supported
but I still can't figure out how to show my image.
Does anyone have some ideas? Please share, i'm a newbie with opencv.
image.shape = ()
image.dtype = dtype('O')
Cheers
You need to call get_data() to get the image from the frame.
image = np.asanyarray(ir1_frame.get_data())
I want to load an image then merge that image with realtime video frame. How can I do this with OpenCV? I would prefer to use Python, but am open to other languages.
My code so far is (frame is video capture):
resim = LoadImage('angel.jpg',1)
SetImageROI(frame, (10, 10, resim.width,resim.height))
Add(frame,resim, frame, None)
ResetImageROI(frame)
However, this gives the error
src1.size() == src2.size() && src1.type() == src2.type() && func != 0
Try following code. Change video and image file:
import cv
resim = cv.LoadImage('image.jpg')
capture = cv.CaptureFromFile('video.avi')
while(1):
frame = cv.QueryFrame(capture)
cv.SetImageROI(frame,(100,100,resim.width,resim.height))
cv.Add(frame,resim,frame)
cv.ResetImageROI(frame)
cv.ShowImage('frame',frame)
if cv.WaitKey(33)==27:
break