iT邦幫忙

0

Flask網頁

  • 分享至 

  • xImage

今天我想要偵測到我的物件後,可以原頁面跳轉到product_page.html。但現在是有偵測到也有掃描紀錄,但就是不會跳轉而且偵測的部分就直接卡住,我vscode裡面也沒有顯示錯誤。而我的html也都丟在資料夾templates內了

我掃描的頁面python

from flask import Flask, render_template, Response, request, redirect
import cv2
import numpy as np
import torch
import time
import webbrowser

app = Flask(__name__)

model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True)

cap = cv2.VideoCapture(0)
height, width, _ = cap.read()[1].shape
baseline = height // 2 + 100

# 存儲已跳轉過的網頁
opened_links = []

video_paused = False

detected_object_names = []  # 使用列表來存儲檢測到的物體名稱


def detect_objects():
    while cap.isOpened():
        if video_paused:
            time.sleep(0.1)
            continue

        success, frame = cap.read()
        if not success:
            print("Ignoring empty camera frame.")
            continue
        frame = cv2.resize(frame, (800, 480))

        results = model(frame)
        detected_objects = results.xyxy[0]
        cv2.line(frame, (0, baseline), (width, baseline), (0, 0, 255), thickness=2)

        for obj in detected_objects:
            obj_class = int(obj[-1])
            if obj_class in [0, 1, 2]:
                obj_center_x = (obj[0] + obj[2]) / 2 + 100
                obj_center_y = (obj[1] + obj[3]) / 2 + 100
                if obj_center_y > baseline:
                    link_url = ""
                    if obj_class == 0:
                        link_url = "/product_page"
                        detected_object_name = "柚香"
                    elif obj_class == 1:
                        link_url = "/product_page"
                        detected_object_name = "福樂"
                    elif obj_class == 2:
                        link_url = "/product_page"
                        detected_object_name = "美祿"

                    if link_url not in opened_links:
                        opened_links.append(link_url)
                        detected_object_names.append(detected_object_name)
                        return redirect(link_url)

        img_bytes = cv2.imencode('.jpg', np.squeeze(results.render()))[1].tobytes()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + img_bytes + b'\r\n')


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


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


@app.route('/start_video', methods=['POST'])
def start_video():
    global video_paused
    video_paused = False
    return '已啟動'


@app.route('/stop_video', methods=['POST'])
def stop_video():
    global video_paused
    video_paused = True
    cap.release()
    cv2.destroyAllWindows()
    return '已停止'


@app.route('/pause_video', methods=['POST'])
def pause_video():
    global video_paused
    video_paused = True
    return '已暂停'


@app.route('/resume_video', methods=['POST'])
def resume_video():
    global video_paused
    video_paused = False
    return '已繼續'

@app.route('/other_page')
def other_page():
    return render_template('other_page.html', object_names=detected_object_names)

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

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

我要跳轉到的html

<!DOCTYPE html>
<html>
<head>
    <title>我的網頁</title>
</head>
<body>
    <h1>歡迎來到我的網頁!</h1>
</body>
</html>

echochio iT邦高手 1 級 ‧ 2023-05-17 09:44:32 檢舉
加 Log 去 debug....
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答