kostya-cholak commited on
Commit
34001e2
·
1 Parent(s): 41c19be

fix: update temp file logic

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -29,16 +29,15 @@ def load_model():
29
  return YOLO(new_cached_model_path)
30
 
31
 
32
- def process_video(video_path):
33
  cap = cv2.VideoCapture(video_path)
34
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
35
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
36
  fps = int(cap.get(cv2.CAP_PROP_FPS))
37
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
38
-
39
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
40
  fourcc = cv2.VideoWriter_fourcc(*'avc1')
41
- out = cv2.VideoWriter(temp_file.name, fourcc, fps, (width, height))
42
 
43
  progress_text = "Processing video... Please wait."
44
  progress_bar = st.progress(0)
@@ -72,7 +71,6 @@ def process_video(video_path):
72
  progress_bar.empty()
73
  status_text.text(f"Processed {total_frames} frames")
74
  time_text.text(f"Total time: {time.time() - start_time:.2f}s")
75
- return temp_file.name
76
 
77
 
78
  model = load_model()
@@ -102,15 +100,17 @@ if 'video_path' in locals():
102
  st.video(video_path)
103
 
104
  if st.button("Detect"):
 
 
105
  with st.spinner("Processing video..."):
106
- processed_video_path = process_video(video_path)
107
 
108
  st.success("Video processing complete!")
109
  st.header("Processed Video")
110
- st.video(processed_video_path)
111
 
112
  # Add download link
113
- with open(processed_video_path, "rb") as file:
114
  btn = st.download_button(
115
  label="Download Video",
116
  data=file,
@@ -118,8 +118,8 @@ if 'video_path' in locals():
118
  mime="video/mp4"
119
  )
120
 
121
- # Clean up temporary files
122
- os.unlink(processed_video_path)
123
  if video_option == "Upload video":
124
  os.unlink(video_path)
125
 
 
29
  return YOLO(new_cached_model_path)
30
 
31
 
32
+ def process_video(video_path, output_path):
33
  cap = cv2.VideoCapture(video_path)
34
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
35
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
36
  fps = int(cap.get(cv2.CAP_PROP_FPS))
37
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
38
+
 
39
  fourcc = cv2.VideoWriter_fourcc(*'avc1')
40
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
41
 
42
  progress_text = "Processing video... Please wait."
43
  progress_bar = st.progress(0)
 
71
  progress_bar.empty()
72
  status_text.text(f"Processed {total_frames} frames")
73
  time_text.text(f"Total time: {time.time() - start_time:.2f}s")
 
74
 
75
 
76
  model = load_model()
 
100
  st.video(video_path)
101
 
102
  if st.button("Detect"):
103
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
104
+
105
  with st.spinner("Processing video..."):
106
+ process_video(video_path, temp_file.name)
107
 
108
  st.success("Video processing complete!")
109
  st.header("Processed Video")
110
+ st.video(temp_file.name)
111
 
112
  # Add download link
113
+ with open(temp_file.name, "rb") as file:
114
  btn = st.download_button(
115
  label="Download Video",
116
  data=file,
 
118
  mime="video/mp4"
119
  )
120
 
121
+ # # Clean up temporary files
122
+ # os.unlink(temp_file.name)
123
  if video_option == "Upload video":
124
  os.unlink(video_path)
125