1

I'm new to the python world and I have an RPI project where I would like to display the camera's (Arducam 16mpx) realtime image into my GUI application. I saw multiple projects web streaming with OpenCV and Tkinter but I just want to feed it into my app. Is there any way to do it simply with Picamera2 and Guizero?

Thank you

2 Answers 2

0

Yes, it is possible to display the real-time image from the Arducam 16mpx camera in your GUI application using the Picamera and Guizero libraries in Python.

To do this, you will first need to install the Picamera and Guizero libraries. You can do this by running the following command in your terminal:

pip install picamera guizero

Once you have the libraries installed, you can use the Picamera library to capture images from the camera and the Guizero library to display them in your GUI application. Here is some sample code that demonstrates how to capture an image from the camera and display it in a Guizero window:

from picamera import PiCamera
from guizero import App, Picture

camera = PiCamera()

def update_image():
    camera.capture('image.jpg')
    image.value = 'image.jpg'

app = App()
image = Picture(app, image='image.jpg', align='left')
app.repeat(1000, update_image)
app.display()

This code will create a new Guizero App and Picture widget and in the update_image function will capture the image from the camera and set the value of the Picture widget to the captured image. The app.repeat(1000, update_image) line will run the update_image function every 1000 milliseconds (1 second), updating the image in the GUI.

You'll need to adjust the code as per your requirement and test it on your Arducam 16mpx camera.

In order to make the code work with your Arducam 16mpx camera you will need to make sure that the camera is properly connected and configured. You can do this by using the camera.start_preview() method to open a preview window and check that the camera is working as expected.

In addition, you may need to adjust the camera's resolution and other settings to match the specifications of your Arducam 16mpx camera. You can do this using the camera.resolution and other properties of the PiCamera class.

You will also need to ensure that you have the required libraries and drivers installed for your camera to work with the PiCamera library.

Here is an example of how you can use the camera.resolution property to set the resolution of the camera to 16mpx :

camera = PiCamera()
camera.resolution = (4608, 3456)

You can also set the image format and other properties such as brightness, contrast, etc.

If you want to stream video from the camera, you will need to use the

PiCamera.start_recording()

method to start streaming video and the

PiCamera.stop_recording()

method to stop streaming video.

Here is an example of how you can use OpenCV to display the video stream from the camera:

import cv2
from picamera.array import PiRGBArray
from picamera import PiCamera

camera = PiCamera()
camera.resolution = (640, 480)
rawCapture = PiRGBArray(camera, size=(640, 480))

cv2.namedWindow("Frame", cv2.WINDOW_NORMAL)

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    image = frame.array
    cv2.imshow("Frame", image)
    key = cv2.waitKey(1)
    rawCapture.truncate(0)

    if key == ord("q"):
        break

cv2.destroyAllWindows()
camera.close()

This code will create a new PiCamera object, set the resolution to 640x480, and create a PiRGBArray object to store the video frames. It then creates a new OpenCV window called "Frame" and starts a loop that captures video frames and displays them in the window. The loop continues until the 'q' key is pressed.

You can adjust the resolution of the camera to match the specifications of your Arducam 16mpx camera and also you can add a Guizero App in order to show the video stream in your GUI application.

4
  • 1
    Please don't use ChatGPT, it just generates nonsense that sounds sensible. Commented Jan 25, 2023 at 21:16
  • 1
    Welcome -- but Stack Exchange does not allow chat GPT: meta.stackoverflow.com/questions/421831/… However, it's not clear how much of this was generated, and a quick cursory read implies to me it is reasonable if somewhat fluffily long-winded. For that reason, I have removed your disclaimer and I hope that if there are parts of this you don't know the truth or falsity of, you will remove those too. In future, if you want to use chat GPT to assist your research, great, but please do not post any of its output verbatim.
    – goldilocks
    Commented Jan 25, 2023 at 22:18
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jan 25, 2023 at 22:28
  • Thank you, i will try it but the first one code just displays the taken image so i think i can't stream into guizero Commented Jan 26, 2023 at 21:11
0

For those using the Raspberry Pi Camera functionality in 2023, I had the following documentation of my setup. For reference, I have a Raspberry Pi Model 2 B and I went from installing the OS to using OpenCV on Tkinter with this process.

Commands ran:

        - sudo rasp-config
            - (You will need to turn the legacy camera on)
            - (I had to toggle this a couple of times)
        - sudo apt-get install python3-pil python3-pil.imagetk
        - sudo apt  install python3-matplotlib python3-tk
        - sudo pip3 install pyzmq
        - pip install opencv-contrib-python==4.5.3.56 
            - (This installation took a long time; upgrade pip if possible)
            - (The version that you install matters)
            - (I'd like to reinforce that this took forever)
        - pip install -U numpy
        - sudo apt install -y python3-picamera2

A test file that you can run that check to make sure that your camera is working with Tkinter correctly, use the following code:

'''
    CAMERA GUI
------------------
Instructions:
1. Ensure a version of python has been installed (created on Python 3.11.4)
2. Run the command "pip install Pillow"
3. Run the command "pip install opencv-python" 
4. Run the GUI; "python ./MainFunction.py"
------------------

'''

import tkinter
import cv2
import PIL.Image, PIL.ImageTk
import time

# GUI class for basic webcam functionality
# Sample class instantiation: App(tkinter.Tk(),"tkinter ad OpenCV")
class App:

    def __init__(self, window, window_title, video_source=0):
        
        # Sets up the tkinter window
        self.window=window
        self.window.title=(window_title)

        # Creates instance variable of parameter video_source
        self.video_source=video_source

        # Adds the video capturing component to the canvas
        # Then packs the canvas to the frame
        self.vid= MyVideoCapture(self.video_source)
        self.canvas=tkinter.Canvas(window, width=self.vid.width, height =  self.vid.height)
        self.canvas.pack()

        # Frame for the buttons
        btn_frame=tkinter.Frame(window, background=self.from_rgb((117, 123, 129)))
        btn_frame.place(x=0,y=0, anchor="nw", width=self.vid.width+4)

        # Snapshot button
        self.btn_snapshot=tkinter.Button(btn_frame, text="Snapshot",width=20, command=self.snapshot, bg=self.from_rgb((52, 61, 70)), fg="white")
        self.btn_snapshot.pack(side="left", padx=10, pady=10)

        # Proses button
        # Empty command; could be linked with more features
        self.btn_proses=tkinter.Button(btn_frame, text="Proses", width=10, command=None, bg=self.from_rgb((52, 61, 70)), fg="white")
        self.btn_proses.pack(side="left", padx=10, pady=10)

        # About button
        # Empty command; could be linked with more features
        self.btn_about=tkinter.Button(btn_frame, text="About", width=10, command=None, bg=self.from_rgb((52, 61, 70)), fg="white")
        self.btn_about.pack(side="right", padx=10, pady=10)

        # How long in between photo-frames on the GUI
        self.delay=15

        self.update()

        # Required for tkinter to stay open
        # Closing the mainloop will close the program
        self.window.mainloop()

    # Takes a snapshot of the video
    # Saves in the same directory as the 
    def snapshot(self):
        ret, frame=self.vid.get_frame()

        if ret:
            # Writes the image to a file with a name that includes the date
            # TODO Change this to be a more readable file name later
            cv2.imwrite("frame-"+time.strftime("%d-%m-%Y-%H-%M-%S") + ".jpg", cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) )
        else:
            print("\nUnable to take a snapshot.\n")

    # This is the key method to show the active camera on the GUI
    # Basically gets an image from the camera and updates the PIL Image Canvas
    # Updates the canvas after "self.delay" milliseconds
    def update(self):
        ret, frame=self.vid.get_frame()

        if ret:
            # Updates the canvas on the GUI
            self.photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(frame))
            self.canvas.create_image(0,0, image=self.photo, anchor=tkinter.NW)

            # Recursive method; waits "self.delay" before calling itself
            self.window.after(self.delay,self.update)

    def from_rgb(self,rgb):
        return "#%02x%02x%02x" % rgb

# Class responsible for the video capturing feature
# Requires the import of cv2
class  MyVideoCapture:
    """docstring for  MyVideoCapture"""
    def __init__(self, video_source=0):
        # Instantiating a VideoCapture device from cv2 library
        self.vid = cv2.VideoCapture(video_source)
        
        # Throw an exception if the video source cannot be opened.
        if not self.vid.isOpened():
            raise ValueError("VideoCapture: Unable to open the video source. Check camera.", video_source)

        # Getting the camera width and height to correctly display resolution
        # Important component for a correct display
        self.width = self.vid.get(cv2.CAP_PROP_FRAME_WIDTH)
        self.height = self.vid.get(cv2.CAP_PROP_FRAME_HEIGHT)

    # Gets the VideoCapture video
    # Called by the update() method
    def get_frame(self):
        if self.vid.isOpened():
            ret, frame = self.vid.read()
            if ret:
                return (ret, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
            else:
                return (ret, None)
        else:
            return (ret,None)       
    
    # Closes the video camera with the "release()" command
    # Important for closing gracefully
    def __del__(self):
        if self.vid.isOpened():
            self.vid.release()

# How to instantiate this GUI
App(tkinter.Tk(),"tkinter ad OpenCV")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.