Trying to execute a conditional statement on the basis of value provided by html form.
The flow of the code is as follows. Index.html gets rendered with a videostream and has a checkbox to let the user decide wether they want to run face detection or not.
On pressing the check button flask saves the value to a variable called status, and tries to pass it during the opencv_camera object. All my attempts yield self/status not defined.
Really lost please help.
camera_opencv.py
from base_camera import BaseCamera
**# THE CLASS IN QUESTION**
class Camera(BaseCamera):
status = 0
def __init__(self, state=None):
if state:
status = state
else:
status = 0
super().__init__()
video_source = 0
@staticmethod
def set_video_source(source):
Camera.video_source = source
@staticmethod
def frames():
camera = cv2.VideoCapture(Camera.video_source)
if not camera.isOpened():
raise RuntimeError('Could not start camera.')
while True:
# read current frame
_, frame = camera.read()
# frame = cv2.resize(frame, (704, 396))
if status: # CONDITIONAL STATEMENT
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
if len(faces) > 0:
print("SUPS")
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
# encode as a jpeg image and return it
yield cv2.imencode('.jpg', frame)[1].tobytes()
app.py
from camera_opencv import Camera
app = Flask(__name__)
state = 0 # STATE VARIABLE
@app.route('/', methods = ['GET','POST'])
def index():
state = request.form.get('check')
# print(state)
# import pdb; pdb.set_trace()
return render_template("index.html")
def gen(camera):
"""Video streaming generator function."""
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/video_feed')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(gen(Camera(state=state)),
mimetype='multipart/x-mixed-replace; boundary=frame')#STATE VARIABLE BEING PASSED