在使用flask框架和yolov5开发html网页时,打开摄像头并进行实时检测是常见的需求。然而,有时会遇到无法成功显示检测框的问题。下面我们将逐步分析问题并提供解决方案。
首先来看一下前端代码:
<body> <div class="row" style="padding:3%;"> <div class="col-lg-6"> <h5>输入数据:</h5> <div> <video id="video" autoplay></video> </div> </div> <div class="col-lg-6"> <h5>输出结果:</h5> <div class="class="custom-file-container__image-preview"> @@##@@ </div> </div> </div> <input type="button" onclick="start()" value="开始识别"> <input type="button" onclick="stop()" value="暂停识别"> <script> function start() { navigator.mediadevices.getusermedia({ video: true }) .then(function (stream) { var video = document.queryselector('video'); video.srcobject = stream; var canvas = document.createelement('canvas'); var ctx = canvas.getcontext('2d'); setinterval(function () { var videowidth = video.videowidth; var videoheight = video.videoheight; canvas.width = videowidth; canvas.height = videoheight; ctx.drawimage(video, 0, 0, videowidth, videoheight); var imagedata = canvas.todataurl('image/png',1); // 压缩图片 // 发送数据到后端 $.ajax({ type: 'post', url: '/image_data', data: { id :$("#uid").val(), image_data: imagedata }, success: function (response) { console.log(response); } }); }, 1000 / 30); // 每秒30帧 }) $("#res").attr("src", "/img_feed?id="+$("#uid").val()) .catch(function (error) { console.error(error); }); } </script>
接着是后端代码:
# 视频推流 def gen(path): cap = cv2.VideoCapture(path) while cap.isOpened(): try: # 记录开始时间 start_time = time.time() # 获取画面 success, frame = cap.read() if success: im, label, c = d.detect(frame) ret, jpeg = cv2.imencode('.png', im) if ret: frame = jpeg.tobytes() # 计算处理时间 elapsed_time = time.time() - start_time print(f"Frame processing time: {elapsed_time:.3f} seconds") yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n') else: break else: break except Exception as e: print(e) continue cap.release() cv2.VideoCapture(path) # 视频流结果 @app.route('/video_feed') def video_feed(): f = request.args.get("f") print(f'upload/{f}') return Response(gen(f'upload/{f}'), mimetype='multipart/x-mixed-replace; boundary=frame') # 前台推流 @app.route('/image_data', methods=["POST"]) def image_data(): image_data = request.form.get('image_data') id = request.form.get('id') image_data = io.BytesIO(base64.b64decode(image_data.split(',')[1])) img = Image.open(image_data) # 对图片进行处理,例如压缩、滤波等 output = io.BytesIO() img.save(output, format='PNG', quality=85) output.seek(0) # 将处理后的图片保存到服务器 img.save(f'upload/temp{id}.png') with open(f'upload/temp{id}.png', 'wb') as f: f.write(output.read()) return "ok"
用户反馈说在打开摄像头时无法显示检测框,并希望能够正确识别图像的置信度。
问题的关键在于以下几点:
立即学习“前端免费学习笔记(深入)”;
摄像头路径问题:
在 cv2.videocapture(path) 中,path 参数需要正确设置。可以是以下几种情况:
但是在你的代码中,gen(f'upload/{f}') 通过接口传递的 f 是什么?这需要明确。
为了解决这个问题,我们可以采取以下步骤:
通过上述步骤,应该能够解决在打开摄像头时无法显示检测框的问题,并正确识别图像的置信度。
以上就是如何在Flask和YOLOv5开发的HTML网页上解决摄像头无法显示检测框的问题?的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号