Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Low FPS while Recording Frames and Key strokes #102

Closed
CT83 opened this issue Apr 16, 2018 · 2 comments
Closed

Low FPS while Recording Frames and Key strokes #102

CT83 opened this issue Apr 16, 2018 · 2 comments

Comments

@CT83
Copy link

CT83 commented Apr 16, 2018

I currently using the code below to stream and receive frames from my Raspberry Pi to my PC.

Client


import io
import socket
import struct
import time

import picamera

client_socket = socket.socket()
client_socket.connect(('my_server', 8000))

connection = client_socket.makefile('wb')
try:
    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        camera.start_preview()
        time.sleep(2)

        start = time.time()
        stream = io.BytesIO()
        for _ in camera.capture_continuous(stream, 'jpeg'):
            connection.write(struct.pack('<L', stream.tell()))
            connection.flush()
            stream.seek(0)
            connection.write(stream.read())
            stream.seek(0)
            stream.truncate()
    connection.write(struct.pack('<L', 0))
finally:
    connection.close()
    client_socket.close()

Server

import io
import socket
import struct

import numpy as np
from cv2 import cv2

server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8000))
server_socket.listen(0)

connection = server_socket.accept()[0].makefile('rb')
try:
    while True:
        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)
        file_bytes = np.asarray(bytearray(image_stream.read()), dtype=np.uint8)
        img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)

        cv2.imshow('Original', img)
        cv2.waitKey()
finally:
    connection.close()
    server_socket.close()

The code is similar to yours, I got it off PiCamera Repo.
But my Frames per second are really slow, almost 3-5 FPS. How can I solve this problem? Decreasing camera resolution didn't help either.
How did you overcome this problem? What were your FPS like? Should I just ignore this can collect more training data to compensate for it?

@hamuchiwa
Copy link
Owner

I referenced PiCamera doc too, I was able to get 10~15 frames with resolution of 320x240.

@CT83
Copy link
Author

CT83 commented Apr 17, 2018

I don't know where I copied the code from but, I had used
for _ in camera.capture_continuous(stream, 'jpeg'):
instead of
for _ in camera.capture_continuous(stream, 'jpeg', use_video_port=True):
That was the source of the problem!! Getting smooth 25-30 FPS!

@CT83 CT83 closed this as completed Apr 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants