Accessing webcam and videos from device in a render web service

52 Views Asked by At

#Creating a custom YOLOv8 object detection problem and created the back-end using html and created a #flask app. The flaskapp inputs a video or the webcam and performs object detection and then outputs #it on a different page. The whole model works perfectly using local host but after deploying the #app using render, the inputted video doesn't get outputted

#WARNING ⚠️ 'source' is missing. Using 'source=/opt/render/project/src/.venv/lib/python3.7/site-#packages/ultralytics/assets'. #[ERROR:[email protected]] global cap.cpp:164 open VIDEOIO(CV_IMAGES): raised OpenCV exception: #OpenCV(4.8.0) /io/opencv/modules/videoio/src/cap_images.cpp:293: error: (-215:Assertion failed) #!_filename.empty() in function 'open'.

#app.py

app.config['UPLOAD_FOLDER'] = 'static/images' #Generate_frames function takes path of input video file and gives us the output with bounding boxes

around detected objects

#Use FlaskForm to get input video file from user

class UploadFileForm(FlaskForm):
    #We store the uploaded video file path in the FileField in the variable file
    #We have added validators to make sure the user inputs the video in the valid format  and user does upload the
    #video when prompted to do so
    file = FileField("File",validators=[InputRequired()])
    submit = SubmitField("Run")
#Now we will display the output video with detection
def generate_frames(path_x = ''):
 # yolo_output variable stores the output for each detection
 # the output with bounding box around detected objects
  yolo_output = video_detection(path_x)
  for detection_ in yolo_output:
   ref,buffer=cv2.imencode('.jpg',detection_)
   # Any Flask application requires the encoded image to be converted into bytes
   #We will display the individual frames using Yield keyword,
   #we will loop over all individual frames and display them as video
   #When we want the individual frames to be replaced by the subsequent frames the Content-Type, or Mini-Type
   #will be used
   frame=buffer.tobytes()
   yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame +b'\r\n')
def generate_frames_web(path_x):
 yolo_output = video_detection(path_x)
 for detection_ in yolo_output:
  ref,buffer=cv2.imencode('.jpg',detection_)
  frame=buffer.tobytes()
  yield (b'--frame\r\n'
  b'Content-Type: image/jpeg\r\n\r\n' + frame +b'\r\n')
@app.route('/', methods=['GET','POST'])
@app.route('/home',methods=['GET','POST'])
def home():. secure_filename(file.filename))
 return render_template('videoprojectnew.html', form=form)
@app.route('/video')
def video():
 return Response(generate_frames(path_x = session.get('video_path', None)),mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/webapp')
def webapp():
  return Response(generate_frames_web(path_x=0), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route("/webcam", methods=['GET','POST'])
def webcam():
 session.clear()
 return render_template('ui.html')`
0

There are 0 best solutions below