remote-opencv-streaming-live-video icon indicating copy to clipboard operation
remote-opencv-streaming-live-video copied to clipboard

I want to return frames to the client instead of showing them in the browser.

Open mle-usti opened this issue 3 years ago • 6 comments

Hey, I am working on a realtime object detection API, I really like your work, but I need the frames from flask to be returned to the client, whether the client is using an iPhone, an Android or just a python script as client.

mle-usti avatar Jul 09 '21 06:07 mle-usti

Running both processes in the same machine is not an option ?

codectl avatar Jul 11 '21 15:07 codectl

Nope, I need the stream from the client's machine to be sent to my server and then processed there and returned back to the client.

mle-usti avatar Jul 12 '21 04:07 mle-usti

Your client is the process reading the frames from the camera so technically the client has direct access to those frames . If that is not the setup you looking for perhaps lay out some details on what you trying to achieve to see how to go about that.

codectl avatar Jul 15 '21 16:07 codectl

I am working on an object detection API in Flask and I want to get a real-time video stream from the client - which might be from an Android app, an iOS app, or just from a python script running on Windows/Linux - so that I can detect objects in real-time with the detection model (yolov4) on the remote server with Flask. I know it can be converted to tflite but I don't want to use that.

After some work: I was able to do this in python with Server.py running on a remote machine:

Client.py

def RT_Rec():
    camera = cv2.VideoCapture(0)
    try:
        while True:
            ret, frame = camera.read()
            content_type = 'image/jpeg'
            headers = {'content-type': content_type}
            url = "https://[REMOTEMACHINEIP]/RealTime"
            _, img_encoded = cv2.imencode('.jpg', frame)       
            response = requests.post(url, data=img_encoded.tobytes(), headers= headers)
            if b"[SUCCESS]" in response.content or b"[ERROR]" in response.content :
                print(response.content.decode('utf-8'))
                break
            else:
                nparr = np.frombuffer(response.content,np.uint8)
                img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
                cv2.imshow("camera", img)
            if cv2.waitKey(1) & 0xFF == ord("q"):
                break
    except Exception as e:
        print(e)
    finally:
        camera.release()
        cv2.destroyAllWindows()

Server.py

@endpoints.route("/RealTime",methods=["GET","POST"])
def realTime():
    if request.method == "POST":
        img = request.data
        nparr = np.fromstring(img,np.uint8)
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        det,outImg = processFrame(img)
        if det:
            msg = "[SUCCESS] Processed"
            return msg
        _, img_encoded = cv2.imencode('.jpg', outImg)
        return img_encoded.tobytes()
    else:
        return "[ERROR] Invalid Request", 500

Now I what I am looking for is to implement the same thing for an Android/iOS app

mle-usti avatar Jul 16 '21 05:07 mle-usti

@mle-usti I am interested in your work. I tried to replicate what you did there by hosting the server script at pythonanywhere.com and having the client in my PC. But I am facing some issues getting it working. Can you plese help me there?

umairahmadh avatar Aug 26 '21 17:08 umairahmadh

@mle-usti I am also feeling the same which is mentioned above. Could you please solve the issue and give us a solution?

yaswanthsai002 avatar Dec 12 '22 15:12 yaswanthsai002