Automatically refresh an image in ubuntu raspberry pi - python

I am streaming and writing an image to a particular location in a raspberry pi. Every time a new image comes it overwrites the previous one. Now if i keep that image file open, it does not get automatically updated.I have to close and reopen it for the update to happen.Is there anyway i can automatically refresh it.
I tried implementing a python code to continuously read and show the image. But still i have to refresh the window for the image to get updated. Below is the code that i used.
img = cv2.imread("Filename",1)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Please suggest any alternatives. I just need to preview the stream.

You could use a simple loop as you can call imshow repeatedly without destroying it.
while True: #Find something to get out of here
img = cv2.imread("Filename",1)
cv2.imshow('image', img)
cv2.waitKey(1)
cv2.destroyAllWindows()

due to opencv documenation:
For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame for 25 ms, after which display will be automatically closed.
So you have to set time in milliseconds instead of 0.

Related

Python Opencv2 imshow is closing immediately even with waitKey(0)

I'm using wsl2 and VScode as the editor. The code in question is simply:
image = cv2.imread('sample.png')
cv2.imshow('image', image)
cv2.waitKey(0)
The first run goes smoothly and lets me inspect the image until I press a button. However after the first run the picture shows up for a quarter of a sec and then disappears. Any idea what could be causing this?
However after the first run the picture shows up for a quarter of a sec and then disappears.
This appears to be a problem triggered by the first run. Could it be that you'll need to add cv2.destroyAllWindows() to the end of your code?
import cv2
image = cv2.imread('sample.png')
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
I don't know the problem but a workaround worked for me.
I don't press any key to close the OpenCV window. I kill the terminal using the 🗑️ button in the VSCode terminal.
Please consider if even 1 time you press any key to close the OpenCV window, then you have to restart your WSL. So, from the beginning, just kill the terminal instead of pressing any key.
But this is just a temporary workaround. I hope other people can help to find the root cause.
I had the same issue and was able to fix it by updating my WSL version here:
aka.ms/wslstorepage

OpenCV imshow() prevents Qt / python crashing

I'm writing a GUI using python and pyQt to read in data packets through UDP socket, processing it through OpenCV, then finally showing it as real time images using Qt. I created a UDP socket outside the while loop, while using the sock.recvfrom method to read in data packets inside the while loop. Within the same while loop, i processed the data and put it into OpenCV format and use OpenCV imshow() method to show the real time video for experimenting. Everything's great and working smooth, but when i try to show the video through QLabel using QImage and QPixmap, things went bizarre. If OpenCV imshow() exist, the code works fine with additional QPixmap shown in the QLabel on top of the OpenCV cv2.imshow() window. However, if i take out the OpenCV imshow(), the UI will freeze and nothing showed leading "python not responding". I've not yet come up with a good reason why this is happening, and i also tried keeping/changing cv2.waitkey() time without succeeding. Any help would be appreciated.
import socket
import cv2
from PyQt4 import QtCore, QtGui, uic
while True:
data, addr = self.sock.recvfrom(10240)
# after some processing on data to get im_data ...
self.im_data_color_resized = cv2.resize(im_data, (0, 0), interpolation = True)
# using OpenCV to show the video (the entire code works with cv2.imshow but not without it)
cv2.imshow('Real Time Image', self.im_data_color_resized)
cv2.waitKey(10)
# using QLabel to show the video
qtimage = cv2.cvtColor(self.im_data_color_resized, cv2.COLOR_BGR2RGB)
height, width, bpc = qtimage.shape
bpl = bpc * width
qimage = QtGui.QImage(qtimage.data, width, height, bpl, QtGui.QImage.Format_RGB888)
self.imageViewer_label.setPixmap(QtGui.QPixmap.fromImage(qimage))
You need to refresh the event queue, so that your GUI can be updated. Add QtGui.QApplication.processEvents() after the setPixamp function.
It works with cv2.waitKey() because it internally already refreshes the painting events allowing the Qt GUI to be refreshed. But I recommend not to rely on this hack, and explicitly refresh the Qt events with processEvents.
You may also want to put this processing loop in its own thread to leave the GUI/Main thread responsive.

How To Save Image Via Image Processing [Raspberry Pi]

I am creating a project with python and Raspberry Pi. I am trying to use my Webcam, as I, unfortunately burned my Camera Module. I was following along: https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/image_processing/
Everything is working fine, except for one problem. I am not able to save the image file that is captured. I would like to take the photo I have created and turn it into a .jpg image. Code I have currently:
from imgproc import *
import cv2
# open the webcam
my_camera = Camera(320, 240)
# grab an image from the camera
my_image = my_camera.grabImage()
# open a view, setting the view to the size of the captured image
my_view = Viewer(my_image.width, my_image.height, "ETSBot Look")
# display the image on the screen
my_view.displayImage(my_image)
# wait for 5 seconds, so we can see the image
waitTime(0)
Can someone please help me with this problem?
Thanks in advance!
-Saurish Srivastava
Custom Library: https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/downloads/
UPDATE: It does not have to just use this type of code. You can give me an example with a different software. Just tell me how to use it properly so I don't mess up.
Adding the following in your code should save the image in the array my_image as picture.jpg
cv2.imwrite('picture.jpg', my_image)
For details on configuring raspberry pi-http://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/

Overwrite image in PIL instead of opening new window

I'm sending a stream of JPEG images from a Raspberry Pi to my MBP via a simple socket programme in Python 2.7.
When I read the image from the stream on my MBP, it opens up in Preview and opens a new Preview window for every separate image. I have an fps of about 2/3 and obviously 2/3 new windows per second is impossible to work with.
How can I go about only opening one Preview window and simply overwriting the displayed image? Would OpenCV be the best way to go? If so I am unsure how to.
Here is how I read the stream and display the images:
image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0]
if not image_len:
break
image_stream = io.BytesIO()
image_stream.write(connection.read(image_len))
image_stream.seek(0)
image = Image.open(image_stream)
image.show()
OS X Preview seems to automatically reload open images at intervals (always when the window receives focus), but Image.show saves a new temporary file each time you use it. I suggest saving each new frame to the same file and then using subprocess.call with the OS X open command.
This being said, the documentation notes that Image.show is primarily for debugging purposes. For a video with more than a few FPS, you probably want something else. One solution would be an HTML interface with WebSockets, perhaps using something like AutoBahn.

How do can I use OpenCV imshow to display the most recent image in a loop without keyboard feedback

I am running an image processing loop with OpenCV in Python and I would like to display the most recent image mask in my imshow window. So whenever the loop calculates a new mask, it updates the imshow window (at about 6Hz). However, I can't get imshow to return control without waiting for a keyboard interrupt. Any suggestions? Is there a better library to use for this?
Without code this is a guess. BUT!
I imagine you are currently using cv2.waitKey to wait until there is a keyboard input:
cv2.waitKey(33)
if k==27: # Esc key to stop
break
What you need to do is use cv2.waitKey to wait a set amount of time, say 1 ms.
# Wait 1 milliseconds. Specifying 0 means forever, so we don't want that
cv2.waitKey(1)
What you could do is log the image to a folder in the loop's directory if it's not totally essential for the image to be displayed in real time.
cv2.imwrite('image_logs/image_' + str(image_count) + '.jpeg', image)
For instance. Keeping track of images can easily be done with a counter.
You could also use waitkey - which will delay for the number of milliseconds in the parenthesis. Sometimes I have problems with this though (I use an rpi, quite slow!) so I tend to go for the logging option.
cv2.waitKey(50) #wait for 50ms

Categories

Resources