-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
175 lines (123 loc) · 4.76 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import io
import os
import cv2
import time
import base64
from PIL import Image as PILImage
from fastapi.responses import HTMLResponse, StreamingResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi import FastAPI, HTTPException, Request, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from utils.inference_utils import predict
from utils.model_utils import extract_bndbox_values
from dotenv import load_dotenv
from video_predict import gen_streaming_frames, gen_video_chunks
# Load the .env file
load_dotenv()
templates = Jinja2Templates(directory="templates")
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
#app.mount("/static/example", StaticFiles(directory="example"), name="example")
origins = [
"*",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
return templates.TemplateResponse(
"item.html", {"request": request, "env_var": os.environ["DEPLOYMENT_URL"]}
)
@app.post("/prediction")
async def get_prediction(image: UploadFile, annotations: UploadFile):
start_time = time.time()
# Read bites image to Pillow image
image_obj = PILImage.open(io.BytesIO(await image.read()))
# Read annotations
print("Encoding data time: %s seconds" % (time.time() - start_time))
# Get predicted image
start_time = time.time()
annotation_file = io.BytesIO(await annotations.read())
bnbx_values = extract_bndbox_values(annotation_file)
result_image = predict(
image_obj, bnbx_values, video=False, batch_size=8, threshhold=0.6
)
print("Get prediction time: %s seconds" % (time.time() - start_time))
start_time = time.time()
# Encode image to display it back in the interface
_, encoded_img = cv2.imencode(".JPEG", result_image)
# encoded_img = base64.b64encode(encoded_img)
encoded_img_base64 = base64.b64encode(encoded_img)
print("Decoding data time: %s seconds" % (time.time() - start_time))
# return encoded_img_base64
return {
"encoded_img": encoded_img_base64,
}
@app.get("/video")
def get_video():
file_path = "./example/parking_video.mp4" # replace with your video file path
# Open video file in binary mode
def iterfile():
with open(file_path, mode="rb") as file_like:
yield from file_like
# Return a streaming response
return StreamingResponse(iterfile(), media_type="video/mp4")
@app.get("/video")
def get_video():
file_path = "video_chunks\output_chunk.mp4"
# Return a streaming response
return FileResponse(file_path, media_type="video/mp4")
@app.get("/video_chunk/{start_second}/parking_video.mp4")
async def get_video_chunk(start_second: int):
video_path = f"video_chunks\output_chunk_st_{start_second}_end_{start_second+5}.mp4"
if not os.path.exists(video_path):
print("video chunk does not exist, generating...")
annotation_path = "./example/pg_survailance.xml"
print(f"Starting from second {start_second}")
bndbox_values = extract_bndbox_values(annotation_path)
gen_video_chunks(
"./example/parking_video.mp4",
step=60,
bndbox_values=bndbox_values,
start_second=start_second,
)
# Check if the file exists
if not os.path.exists(video_path):
raise HTTPException(status_code=404, detail="Video not found")
# Serve the video file
return FileResponse(video_path, media_type="video/mp4")
@app.get("/video_pred")
def get_video():
video_path = "./example/parking_video.mp4"
annotation_path = "./example/pg_survailance.xml"
bndbox_values = extract_bndbox_values(annotation_path)
# Return a streaming response
return StreamingResponse(
gen_streaming_frames(video_path, step=60, bndbox_values=bndbox_values),
media_type="multipart/x-mixed-replace; boundary=frame",
)
@app.get("/video-stream.m3u8")
def video_stream():
return FileResponse("stream/stream.m3u8")
@app.get("/segments/{segment_name}")
def video_segments(segment_name: str):
return FileResponse(f"stream/{segment_name}")
if __name__ == "__main__":
import uvicorn
if os.environ["DEPLOYMENT_URL"] == "http://localhost:8000":
import ngrok
port = 8000
listener = ngrok.forward(
f"http://localhost:{port}",
authtoken_from_env=True,
domain="possum-enough-informally.ngrok-free.app",
)
public_url = listener.url()
print(f"Waiting on public url: {public_url}")
uvicorn.run("main:app", host="localhost", port=port, reload=False)