首页 > 解决方案 > 烧瓶和opencv捕获的Python问题

问题描述

我想从网络摄像头或我的 jetson nano 上的 csi cam 获取信号,将其显示在网络浏览器上,然后按一些按钮来制作快照,并根据按钮将图片放在不同的文件夹中。

我制作了以下代码,它似乎可以工作,但是在几个按钮操作之后,浏览器开始无限期加载。一段时间后,Web 浏览器不会无限期地加载。它加载直到我按下其中一个按钮。在这种情况下,我无法看到来自相机的实时信号,我只是看到按下按钮时拍摄的快照。

from flask import Flask, render_template, Response, request
import cv2
import datetime, time
import os, sys
import numpy as np
from threading import Thread


global rec_frame, Polluted, Clear, Treated, OutOfService 
Polluted=0
Clear=0
Treated=0
OutOfService=0


#instatiate flask app  
app = Flask(__name__, template_folder='./templates')

#select webcam or CSI
#camera = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)960,format=(string)NV12, framerate=(fraction)20/1 ! nvvidconv flip-method=0 ! video/x-raw,format=(string)BGRx ! videoconvert ! video/x-raw,width=(int)1280, height=(int)960, format=(string)BGR ! appsink"
#, cv2.CAP_GSTREAMER)
camera = cv2.VideoCapture(1)

def snapshot(frame,folder):
    cropped = frame[100:400,200:500]
    resized = cv2.resize(cropped,(100,100))
    cv2.imwrite("/home/ava/Documents/AVA/Get pictures/" + folder + "/frame-" + time.strftime("%d-%m-%Y-%H-%M-%S") + ".jpg",cv2.cvtColor(resized,cv2.COLOR_RGB2BGR))



def gen_frames():  # generate frame by frame from camera
    global rec_frame,Polluted,Clear,Treated,OutOfService


    while True:
        success,frame = camera.read() 
        if success:
        #get snapshot if button pressed
            if(Polluted):                
                snapshot(frame,"Polluted Water")
                Polluted = 0 
            if(Clear):
                snapshot(frame,"Cleared Water")
                Clear = 0
            if(Treated):
                snapshot(frame,"Treated Water")
                Treated = 0   
            if(OutOfService):
                snapshot(frame,"Out of Service")
                OutOfService = 0
         
                
            try:
                ret, buffer = cv2.imencode('.jpg', cv2.flip(frame,1))
                frame = buffer.tobytes()
                yield (b'--frame\r\n'
                b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
            except Exception as e:
                print('pass')
                pass
                
        else:
            pass



@app.route('/')
def index():
    return render_template('index.html')

@app.route('/video_feed')
def video_feed():
    return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/requests',methods=['POST','GET'])
def tasks():
    global switch,camera
    if request.method == 'POST':
        if request.form.get('Polluted') == 'Polluted':
            global Polluted,rec_frame
            Polluted=1
            print('in polluted')
        elif  request.form.get('Clear') == 'Clear':
            global Clear
            Clear = 1
        elif  request.form.get('Treated') == 'Treated':
            global Treated
            Treated = 1
        elif  request.form.get('OutOfService') == 'OutOfService':
            global OutOfService
            OutOfService = 1 
                   
    elif request.method=='GET':
        return render_template('index.html')
    return render_template('index.html')

if __name__ == '__main__':
    app.run()

camera.release()
cv2.destroyAllWindows()  

标签: pythonopencvflask

解决方案


推荐阅读