app.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from importlib import import_module
  2. import os
  3. from flask import Flask, render_template, Response, request, send_file
  4. import cv2
  5. import subprocess
  6. import time
  7. import sys
  8. import pdb
  9. # import camera driver
  10. # from object_detection import VideoStreaming
  11. if os.environ.get('CAMERA'):
  12. Camera = import_module('camera_' + os.environ['CAMERA']).Camera
  13. else:
  14. from camera import Camera
  15. app = Flask(__name__)
  16. # def gen(camera):
  17. # while True:
  18. # frame = VideoStreaming.get_frame()
  19. # # cv2.imencode('.jpg', frame)
  20. # yield (b'--frame\r\n'
  21. # b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
  22. class Model():
  23. def __init__(self, device='cuda'):
  24. tstamp = time.time()
  25. self.device = device
  26. self.net =
  27. def get_stream_video():
  28. # camera 정의
  29. cam = cv2.VideoCapture('rtsp://astrodom:hdci12@192.168.170.73:554/stream1')
  30. while True:
  31. # 카메라 값 불러오기
  32. success, frame = cam.read()
  33. # print(frame)
  34. # print(type(frame))
  35. if not success:
  36. break
  37. else:
  38. ret, buffer = cv2.imencode('.jpeg', frame)
  39. # frame을 byte로 변경 후 특정 식??으로 변환 후에
  40. # yield로 하나씩 넘겨준다.
  41. print(type(buffer))
  42. frame = buffer.tobytes()
  43. print(type(frame))
  44. pdb.set_trace()
  45. yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + bytearray(frame) + b'\r\n')
  46. @app.route('/')
  47. def index():
  48. return render_template('index.html')
  49. # 스트리밍 경로를 /video 경로로 설정.
  50. @app.get("/video")
  51. def video():
  52. # StringResponse함수를 return하고,
  53. # 인자로 OpenCV에서 가져온 "바이트"이미지와 type을 명시
  54. return Response(get_stream_video(), mimetype="multipart/x-mixed-replace; boundary=frame")
  55. # ipcam 열기
  56. @app.route("/stream", methods=['GET'])
  57. def stream():
  58. print("here")
  59. subprocess.run(['python3', '/root/helmet_det/yolov7-main/detect.py', '--source', 'rtsp://astrodom:hdci12@192.168.170.73:554/stream1', '--weights', 'best.pt'])
  60. return "done"
  61. # rtsp://astrodom:hdci12@192.168.170.73:554/stream1
  62. if __name__ == '__main__':
  63. app.run(host='0.0.0.0', debug=True)