0

I've developed an application using OpenCV. It uses the camera and detects the face using OpenCV. When I use the app on localhost, it works fine, but when I deploy it on Heroku. The app cannot access the camera of the visitor. How can the camera be accessed by the OpenCV application?

Here's the code.

import cv2

faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')


class Video(object):
    def __init__(self):
        self.video = cv2.VideoCapture(-1)
        self.video.set(3, 1280)
        self.video.set(4, 720)

    def __del__(self):
        self.video.release()

    def get_frame(self):
        ret, frame = self.video.read()
        frame = cv2.flip(frame, 1)
        faces = faceDetect.detectMultiScale(frame, 1.3, 5)
        for x, y, w, h in faces:
            x1, y1 = x + w, y + h
            cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 255), 1)
            cv2.line(frame, (x, y), (x + 30, y), (255, 0, 255), 6)  # Top Left
            cv2.line(frame, (x, y), (x, y + 30), (255, 0, 255), 6)

            cv2.line(frame, (x1, y), (x1 - 30, y), (255, 0, 255), 6)  # Top Right
            cv2.line(frame, (x1, y), (x1, y + 30), (255, 0, 255), 6)

            cv2.line(frame, (x, y1), (x + 30, y1), (255, 0, 255), 6)  # Bottom Left
            cv2.line(frame, (x, y1), (x, y1 - 30), (255, 0, 255), 6)

            cv2.line(frame, (x1, y1), (x1 - 30, y1), (255, 0, 255), 6)  # Bottom right
            cv2.line(frame, (x1, y1), (x1, y1 - 30), (255, 0, 255), 6)

        ret, jpg = cv2.imencode('.jpeg', frame)
        return jpg.tobytes()
stateMachine
  • 4,140
  • 3
  • 11
  • 25
  • 1
    Your Python code is running on a Heroku dyno, not the user's machine. OpenCV won't be able to access any of the user's hardware directly. You might be able to access it with JavaScript and stream that to your server, but that will require substantial changes to your code. – Chris Nov 23 '21 at 12:53
  • Thanks a lot @Chris – Minhaz Uddin Dec 12 '21 at 20:49

0 Answers0