from importlib import import_module import os from flask import Flask, render_template, Response, request, send_file import cv2 import subprocess # import camera driver # from object_detection import VideoStreaming from detect import frame_ # if os.environ.get('CAMERA'): # Camera = import_module('camera_' + os.environ['CAMERA']).Camera # else: # from camera import Camera app = Flask(__name__) # def gen(camera): # while True: # frame = VideoStreaming.get_frame() # # cv2.imencode('.jpg', frame) # yield (b'--frame\r\n' # b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') def get_stream_video(): # camera 정의 cam = cv2.VideoCapture('rtsp://astrodom:hdci12@192.168.170.73:554/stream1') while True: # 카메라 값 불러오기 # f = frame_() # print(f.inference()) success, frame = cam.read() # print(frame) # print(type(frame)) if not success: break else: ret, buffer = cv2.imencode('.jpeg', frame) # frame을 byte로 변경 후 특정 식??으로 변환 후에 # yield로 하나씩 넘겨준다. frame = buffer.tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + bytearray(frame) + b'\r\n') @app.route('/') def index(): return render_template('index.html') # 스트리밍 경로를 /video 경로로 설정. @app.get("/video") def video(): # StringResponse함수를 return하고, # 인자로 OpenCV에서 가져온 "바이트"이미지와 type을 명시 return Response(get_stream_video(), mimetype="multipart/x-mixed-replace; boundary=frame") # ipcam 열기 @app.route("/stream", methods=['GET']) def stream(): print("here") subprocess.run(['python3', '/root/helmet_det/yolov7-main/detect.py', '--source', 'rtsp://astrodom:hdci12@192.168.170.73:554/stream1', '--weights', 'best.pt']) return "done" # rtsp://astrodom:hdci12@192.168.170.73:554/stream1 if __name__ == '__main__': app.run(host='0.0.0.0', debug=True)