-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
58 lines (47 loc) · 1.34 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import base64
from fastapi import FastAPI
from fastapi import File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
import numpy as np
import cv2
import cv2
from http.server import HTTPServer, SimpleHTTPRequestHandler, test
app = FastAPI()
origins = [
"http://localhost",
"http://localhost:3000",
"http://localhost:8000",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class CORSRequestHandler(SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header('Access-Control-Allow-Origin', '*')
SimpleHTTPRequestHandler.end_headers(self)
@app.get('/')
async def read_root():
return {"Message": "Detect Object!"}
@app.get('/detect/')
async def detect_objects():
video_path = 'bottle.mp4'
cap = cv2.VideoCapture(video_path)
width = int(cap.get(3))
height = int(cap.get(4))
if not cap.isOpened():
print("Error opening video stream or file")
# Read the video frames
frame_count = 0
while cap.isOpened():
detections = np.empty((0, 5))
ret, frame = cap.read()
if not ret:
print("Error reading frames")
break
frame = cv2.resize(frame, (640, 480))
code = base64.b64encode(frame)
return {"frame": code}