Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5369

Troubleshooting • Raspberry Pi 5 and USB Camera

$
0
0
Hey,

I'm working on a project with a Raspberry Pi 5 and an external USB camera. My problem is that when I use the USB camera, I'm only getting around 2-2.4 FPS.

Detailed information about my setup:

I'm using Raspberry Pi OS Lite 64-bit
I work with an SSH connection
I use VS Code (with SSH)
Here's what I've done so far:

The camera works perfectly (at 30 FPS) when connected to Windows
The CPU usage of the Pi is around 1%
The same issue occurs when I use another camera (1 FPS), which also works at 30 FPS on Windows
I've tried running it with the original PSU
The problem is the same across all USB slots
The same issue occurs with a Raspberry Pi 4 (around 1 FPS with both cameras, around 1.4% CPU usage)
Here is the code I used to measure the FPS and CPU usage.

Code:

import cv2import timeimport psutildef get_cpu_usage():    # CPU-Auslastung in Prozent    return psutil.cpu_percent(interval=1)def measure_fps(video_source=0):    cap = cv2.VideoCapture(video_source)    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)    cap.set(cv2.CAP_PROP_FPS, 25)    if not cap.isOpened():        print("Error: Cannot open video source.")        return    fps = 0    start_time = time.time()    frame_count = 0    while True:        ret, frame = cap.read()        if not ret:            break        usage = get_cpu_usage()        print(f"Die aktuelle CPU-Auslastung beträgt: {usage:.2f} %")        frame_count += 1        # Calculate FPS every second        elapsed_time = time.time() - start_time        if elapsed_time > 1:            fps = frame_count / elapsed_time            start_time = time.time()            frame_count = 0            print(f"FPS: {fps:.2f}")        # Display the frame        #cv2.imshow('Frame', frame)        if cv2.waitKey(1) & 0xFF == ord('q'):            break    cap.release()    cv2.destroyAllWindows()if __name__ == "__main__":    measure_fps()
I also tried streaming it, and the result is similar—the camera is detected, but I only get around 2 FPS.

Code:

import cv2import psutilfrom flask import Flask, Responseimport numpy as npapp = Flask(__name__)def get_cpu_usage():    # Log CPU usage less frequently, e.g., every 10 seconds    return psutil.cpu_percent(interval=1)def gen_frames():    camera = cv2.VideoCapture(0)        # Set the desired resolution (optional)    camera.set(cv2.CAP_PROP_FRAME_WIDTH, 2048)    camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 1536)    # Reduce JPEG quality to speed up encoding    encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 50]    while True:        success, frame = camera.read()        height, width, channels = frame.shape        if not success:            break        else:            # Encode the frame in JPEG format with reduced quality            ret, buffer = cv2.imencode('.jpg', frame, encode_param)            frame = buffer.tobytes()            # Yield the frame in the MJPEG stream format            yield (b'--frame\r\n'                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')        camera.release()@app.route('/video_feed')def video_feed():    return Response(gen_frames(),                    mimetype='multipart/x-mixed-replace; boundary=frame')if __name__ == '__main__':    app.run(host='0.0.0.0', port=8000, debug=False, threaded=True)
I hope someone has an idea of how I can fix this problem.
Thanks for your help!

Statistics: Posted by UIO8747 — Fri Aug 09, 2024 7:16 pm



Viewing all articles
Browse latest Browse all 5369

Trending Articles