These are my routes and some functions:
from flask import Flask, render_template, Response
import cv2
import time
from sys import stdout
from flask_socketio import SocketIO
import math
import numpy as np
import logging
import os
from camera import VideoCamera
app = Flask(__name__)
app.logger.addHandler(logging.StreamHandler(stdout))
app.config['SECRET_KEY'] = 'b13ce0c6768bb0b280bab13ceb13ce0cde280ba0c676dfde280ba245676dfde280ba0c676dfde280ba245'
app.config['DEBUG'] = True
socketio = SocketIO(app)
@socketio.on('connect', namespace='/test')
def test_connect():
app.logger.info("client connected")
@app.route('/', methods = ['GET','POST'])
def index():
"""Video streaming home page."""
return render_template('index.html')
def gen(camera):
while True:
data= camera.get_frame()
frame=data[0]
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route('/video_feed')
def video_feed():
return Response(gen(VideoCamera()), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/golden_ratio_calculating', methods = ['GET','POST'])
def calculate():
ratio = main()
return render_template('golden_calc_page.html' , ratio123 = ratio)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
socketio.run(app, port = port)
In the code below i have used WebcamVideoStream(src = 0).start(). In this src = 0 works fine on local server but when i deployed it on heroku it not opening the webcam( i.e. it is not detecting the webcam on heroku server). Check here(the webpage): https://golden-ratio-calculator.herokuapp.com/.
import cv2
import pickle
from imutils.video import WebcamVideoStream
# import face_recognition
import time
import math
import random
import numpy as np
class VideoCamera(object):
def __init__(self):
# Using OpenCV to capture from device 0.
self.stream = WebcamVideoStream(src = 0).start()
def __del__(self):
self.stream.stop()
def get_frame(self):
image = self.stream.read()
startTime = time.time()
top2chin = []
left2right = []
top2pupil = []
pupil2lip = []
noseWidth = []
nose2lips = []
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
righteye_cascade = cv2.CascadeClassifier('haarcascade_righteye.xml')
lefteye_cascade = cv2.CascadeClassifier('haarcascade_leftteye.xml')
smile_cascade = cv2.CascadeClassifier('haarcascade_mouth.xml')
nose_cascade = cv2.CascadeClassifier('haarcascade_nose.xml')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
height, width, channels = image.shape
for(x, y, w, h) in faces:
# print("found a face")
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
roi_gray = gray[y:y+h, x:x+h]
roi_color = image[y:y+h, x:x+h]
eyes = eye_cascade.detectMultiScale(roi_gray, 2.5, 5)
smiles = smile_cascade.detectMultiScale(roi_gray, 3.4, 5)
noses = nose_cascade.detectMultiScale(roi_gray, 1.3, 5)
right_eyes = righteye_cascade.detectMultiScale(roi_gray, 2.5, 5)
ex, ey, ew, eh = 0,0,0,0
sx, sy, sw, sh = 0,0,0,0
nx, ny, nw, nh = 0,0,0,0
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 1)
for (sx, sy, sw, sh) in smiles:
cv2.rectangle(roi_color, (sx, sy), (sx+sw, sy+sh), (0, 0, 255), 1)
for (nx, ny, nw, nh) in noses:
cv2.rectangle(roi_color, (nx, ny), (nx+nw, ny+nh), (255, 0, 255), 1)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image, "Hello User", (math.floor(width / 4), math.floor(height / 12)), font, 0.7, (255, 255, 255), 1, cv2.LINE_AA)
ret, jpeg = cv2.imencode('.jpg', image)
data = []
data.append(jpeg.tobytes())
# data.append(name)
return data
Please i tried almost everything. Please help me out to solve it.
Have you had a look at the Heroku docs regarding camera interfaces? There is an add-on CameraTag
Also you might find self-hosting your flask app on an apache2 server a more successful solution. There are many tutorials and the process is not difficult.
Heroku is great, but with my experience with face_recognition specifically, if you don't pay for upgrades you will run into issues exceeding memory <550mb.
This link here is a great tutorial for self-hosting flask app.
Furthermore, it may be that Heroku cannot access the local camera peripheral or the device is no longer [0]
# Using OpenCV to capture from device 0.
self.stream = WebcamVideoStream(src = 0).start()
Have you tried modifying the src?
This may also be useful for your application.