I have a program, which gets video from industrial camera. I use OpenCV in order to display video and do some image analysing. I have several circles displayed on an image and there's a possibility to move them with keyboard. And when I press down a keyboard button and hold it, image acquisition freezes, but moving of a circle still happens (you can see this by unpressing the keyboard button and the circle moved).
Here is a simplified example:
import numpy as np
import cv2
WINDOW_NAME = 'program'
cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)
cv2.setWindowProperty(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_GUI_EXPANDED)
while True:
# Simplified image acquisition.
frame = (np.random.rand(1080, 1920, 3)*255).astype(np.uint8)
cv2.imshow(WINDOW_NAME, frame)
pressed_key_code = cv2.waitKey(1)
if pressed_key_code == ord('q'):
cv2.destroyAllWindows()
break
# Simplified action on key press.
elif pressed_key_code == ord('p'):
print('moving action')
In this example if one presses and holds p the image acquisition stops, but one can see prints still happening.
I tried using different functions for waiting key: cv2.pollKey(), cv2.waitKeyEx(). I used different waiting time. Also after researching the problem, I've found some suggestions on using several cv2.waitKey() functions simultaneously.
I expect to see action not freezing image acquisition. What can I do?
OpenCV version: 4.7.0
Platform: Windows 10.0.17763 AMD64
HighGUI backend: WIN32UI
Related
I've been trying to just take a picture with OpenCV but imshow() returns a single colored image instead.
It doesn't return any Error.
import cv2
from time import sleep
camera = cv2.VideoCapture(0)
ret, frame = camera.read()
sleep(1)
cv2.imshow("frame", frame)
cv2.waitKey(0)
cv2.imwrite("image.jpg", frame)
cv2.destroyAllWindows()
Here is the window created while running this code
My cam is fully working with other programs and python programs, but i can't make this work.
I've tried changing the camera port, rerun pycharm and even restart my computer and cam, but didn't work as well.
When i try to save the image with imwrite() it doesn't work either.
image saved with imwrite()
Help?
EDIT:
It probably was my camera, because i made a program that takes pictures every 5 seconds, and after the second picture, it worked normally.
I am building a stand-alone VR headset using Raspberry Pi 3 model b. I am having a problem with splitting the screen as we see on our phone. I am still learning Python so I don't have much idea on how to do this.
Here in this code, I have tried to solve the above-mentioned problem but when I run this code on Raspbian an error occurs that ImageGrab function works only on Windows and Mac. I tried to use pyscreenshot module also, although it works on my PC screen fairly when I connect it with my 5-inch screen, a black window opens and I see nothing.
import numpy as np
from PIL import ImageGrab
import cv2
import time
while(True):
screen = np.array(ImageGrab.grab(bbox=(920,420,1320,900)))
frame = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)
frame = cv2.resize(frame, (0, 0), None, 1, .83)
numpy_horizontal = np.hstack((frame,frame))
#cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
#cv2.setWindowProperty("window", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow('window',numpy_horizontal)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
Your problem is not splitting a screen, but to display an image on the screen. So you need a library to do that. In your example you are using OpenCV. This is usually a bad choice and only usefull for some simple debugging. You need a proper GUI library.
Here you have a gazillion of options. If you are into games, I would look into moderngl and moderngl-window. This is based on PySide2, and as far as I have seen Raspberry Pi now supports this.
I'm using opencv as part of a beam profiler software. For this I have a high resolution camera (5496x3672, Daheng Imaging MER-2000-19U3M). I'm using now a basic program to show the captured frames. The program works fine for a normal webcam, however when I connect my high resolution camera (through USB 3.0) it becomes buggy. Most of the frame is black and at the top there are three small instances of the recording (screenshot here). On the other hand, the camera software displays the image properly, so I assume there must be a problem in how opencv access the camera. Here is the code used to diplay the image:
import cv2
cap = cv2.VideoCapture(0)
cap.set(3,5496)
cap.set(4,3672)
while(True):
ret, frame = cap.read()
frame2=cv2.resize(frame,(1280,720))
cv2.imshow('frame',frame2)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
I'm using a raspberry pi and trying to create a video stream using flask and the pi camera library. My understanding is that I need to use continuous_capture to get the lowest latency within the system.
However, I can't find a way to preview these images that are supposedly being taken. Is there a way I can view these before I try and implement it into flask which has many issues itself as I will be using the falsk website to control a robot as well.
Any suggestions on how to do this are appreciated as well as telling me there is an easier way to do it. Please note I am only an intermediate programmer so nothing too complex for me to understand as this is the whole idea of the project.
I believe you are looking for capture_continuous.
Here's the general process:
Import the necessary packages
Pause to allow the camera to warm up
Iterate through the camera frames
Capture and show the frame
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
# allow the camera to warmup
time.sleep(0.1)
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
image = frame.array
# show the frame
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
I am trying to activate a pi camera attached to a raspberry module using python. here is my sample code. ON running this code i am getting two windows showing the video. When i tried for processing the video, i get a window of a processed video and one real video. i am jst trying to get a single window. can anyone suggest why is it happening.
import cv2
cap = cv2.VideoCapture(0)
while True:
ret,th = cap.read()
cv2.imshow('video',th)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()