-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·75 lines (64 loc) · 2.48 KB
/
app.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
import cv2
import numpy as np
import torch
import streamlit as st
import streamlit_webrtc as webrtc
import tempfile
from PIL import Image
import onnxruntime
from onnxruntimer import prediction_onnx
import time
opt_session = onnxruntime.SessionOptions()
opt_session.enable_mem_pattern = False
opt_session.enable_cpu_mem_arena = False
opt_session.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_DISABLE_ALL
# model_path = 'models/best.onnx'
EP_list = ['CUDAExecutionProvider', 'CPUExecutionProvider']
ort_session = onnxruntime.InferenceSession('models/yolov8n.onnx', providers=EP_list)
def infer_image(img, model = ort_session):
return prediction_onnx(ort_session=model, image=img)
def main():
DEFAULT_VIDEO_PATH = "data/sample_videos/sample.mp4"
# Create a video file uploader
st.header("Upload a video for inference")
uploaded_file = st.file_uploader("Choose a video...", type=["mp4", "avi", "mov"])
# Create a radio button for selecting between default video and uploaded video
video_selection = st.radio(
"Select video for inference:",
("Use default video", "Use uploaded video")
)
# If the user chooses to use the default video
if video_selection == "Use default video":
video_path = DEFAULT_VIDEO_PATH
# If the user chooses to use the uploaded video
elif video_selection == "Use uploaded video" and uploaded_file is not None:
tfile = tempfile.NamedTemporaryFile(delete=False)
tfile.write(uploaded_file.read())
video_path = tfile.name
# If there's a video to process, do the inference
if video_path is not None:
# Load the video with cv2
cap = cv2.VideoCapture(video_path)
outputing = st.empty()
fps = 0
prev_time = 0
curr_time = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Run the inference
output = infer_image(img=frame, model=ort_session)
# Convert the output to an image that can be displayed
output_image = Image.fromarray(cv2.cvtColor(output, cv2.COLOR_BGR2RGB))
# Display the image
outputing.image(output_image)
curr_time = time.time()
fps = 1 / (curr_time - prev_time)
prev_time = curr_time
# print(fps)
cap.release()
else:
st.write("Please upload a video file or choose to use the default video.")
if __name__ == "__main__":
main()