ModuMLTECH commited on
Commit
e21729d
·
verified ·
1 Parent(s): 4125dc9

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +102 -0
  2. logo.jpg +0 -0
  3. requirements.txt +111 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib
2
+ matplotlib.use('Agg')
3
+
4
+ import streamlit as st
5
+ import cv2
6
+ import numpy as np
7
+ from yolov5 import YOLOv5
8
+ from sort.sort import Sort
9
+ import tempfile
10
+ import shutil
11
+ from moviepy.editor import VideoFileClip, concatenate_videoclips, ImageSequenceClip
12
+ import os
13
+
14
+ # Load the pre-trained model and initialize the SORT tracker
15
+ model_path = 'yolov5s.pt' # Ensure this path points to the model file
16
+ model = YOLOv5(model_path, device='cpu')
17
+ tracker = Sort()
18
+
19
+ def process_video(uploaded_file):
20
+ # Save the uploaded file to a temporary location
21
+ temp_file_path = "temp_video.mp4"
22
+ with open(temp_file_path, "wb") as temp_file:
23
+ temp_file.write(uploaded_file.getbuffer())
24
+
25
+ # Use moviepy to read the video file
26
+ video_clip = VideoFileClip(temp_file_path)
27
+ total_frames = int(video_clip.fps * video_clip.duration)
28
+ width, height = video_clip.size
29
+
30
+ # Temporary directory to save processed video frames
31
+ temp_dir = tempfile.mkdtemp()
32
+
33
+ unique_cars = set()
34
+ progress_bar = st.progress(0)
35
+
36
+ for frame_idx, frame in enumerate(video_clip.iter_frames()):
37
+ frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
38
+
39
+ progress_percentage = int((frame_idx + 1) / total_frames * 100)
40
+ progress_bar.progress(progress_percentage)
41
+
42
+ # Detection and tracking logic
43
+ results = model.predict(frame)
44
+ preds = results.pandas().xyxy[0]
45
+ detections = []
46
+
47
+ for index, row in preds.iterrows():
48
+ if row['name'] == 'car':
49
+ xmin, ymin, xmax, ymax, conf = row['xmin'], row['ymin'], row['xmax'], row['ymax'], row['confidence']
50
+ detections.append([xmin, ymin, xmax, ymax, conf])
51
+
52
+ if detections:
53
+ detections_np = np.array(detections)
54
+ trackers = tracker.update(detections_np)
55
+
56
+ for d in trackers:
57
+ unique_cars.add(int(d[4]))
58
+ xmin, ymin, xmax, ymax = map(int, d[:4])
59
+ cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (255, 0, 0), 2)
60
+ cv2.putText(frame, f'ID: {int(d[4])}', (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
61
+
62
+ cv2.putText(frame, f'Unique Cars: {len(unique_cars)}', (10, 35), cv2.FONT_HERSHEY_SIMPLEX, 1.25, (0, 255, 0), 2)
63
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert back to RGB for moviepy
64
+ cv2.imwrite(f"{temp_dir}/{frame_idx:04d}.jpg", frame)
65
+
66
+ frames_files = [os.path.join(temp_dir, f"{i:04d}.jpg") for i in range(total_frames)]
67
+ clip = ImageSequenceClip(frames_files, fps=video_clip.fps)
68
+ output_video_path = 'processed_video.mp4'
69
+ clip.write_videofile(output_video_path, codec='libx264') # Use libx264 codec for compatibility
70
+
71
+ # Remove temporary directory and temporary files
72
+ shutil.rmtree(temp_dir)
73
+
74
+ return output_video_path
75
+
76
+
77
+ def main():
78
+ # Initialize session state variables if they don't exist
79
+ if 'output_video_path' not in st.session_state:
80
+ st.session_state.output_video_path = None
81
+
82
+ st.sidebar.image("logo.jpg", use_column_width=True)
83
+ uploaded_file = st.sidebar.file_uploader("Upload a video", type=['mp4'])
84
+
85
+ st.title("Car Detection and Tracking")
86
+
87
+ if uploaded_file is not None:
88
+ # Process the video only if it hasn't been processed yet or a new file is uploaded
89
+ if st.session_state.output_video_path is None or st.session_state.uploaded_file_name != uploaded_file.name:
90
+ st.session_state.uploaded_file_name = uploaded_file.name
91
+ st.session_state.output_video_path = process_video(uploaded_file)
92
+
93
+ # Display the processed video
94
+ st.video(st.session_state.output_video_path)
95
+
96
+ # Provide a download link for the processed video
97
+ with open(st.session_state.output_video_path, "rb") as file:
98
+ st.download_button("Download Processed Video", file, file_name="processed_video.mp4")
99
+
100
+
101
+ if __name__ == "__main__":
102
+ main()
logo.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ absl-py==2.1.0
2
+ altair==5.2.0
3
+ attrs==23.2.0
4
+ blinker==1.7.0
5
+ boto3==1.34.64
6
+ botocore==1.34.64
7
+ cachetools==5.3.3
8
+ certifi==2023.7.22
9
+ chardet==4.0.0
10
+ charset-normalizer==3.3.2
11
+ click==8.1.7
12
+ colorama==0.4.6
13
+ contourpy==1.1.1
14
+ cycler==0.10.0
15
+ decorator==4.4.2
16
+ ffmpeg==1.4
17
+ filelock==3.13.1
18
+ filterpy==1.4.5
19
+ fire==0.6.0
20
+ fonttools==4.50.0
21
+ fsspec==2024.3.0
22
+ gitdb==4.0.11
23
+ GitPython==3.1.42
24
+ google-auth==2.28.2
25
+ google-auth-oauthlib==1.0.0
26
+ grpcio==1.62.1
27
+ huggingface-hub==0.21.4
28
+ idna==2.10
29
+ imageio==2.34.0
30
+ imageio-ffmpeg==0.4.9
31
+ importlib_metadata==7.0.2
32
+ importlib_resources==6.3.1
33
+ Jinja2==3.1.3
34
+ jmespath==1.0.1
35
+ jsonschema==4.21.1
36
+ jsonschema-specifications==2023.12.1
37
+ kiwisolver==1.4.5
38
+ lazy_loader==0.3
39
+ Markdown==3.6
40
+ markdown-it-py==3.0.0
41
+ MarkupSafe==2.1.5
42
+ matplotlib==3.7.5
43
+ mdurl==0.1.2
44
+ moviepy==1.0.3
45
+ mpmath==1.3.0
46
+ networkx==3.1
47
+ numpy==1.24.4
48
+ oauthlib==3.2.2
49
+ opencv-python==4.7.0.72
50
+ opencv-python-headless==4.8.0.74
51
+ packaging==23.2
52
+ pandas==2.0.3
53
+ pillow==10.2.0
54
+ pkgutil_resolve_name==1.3.10
55
+ proglog==0.1.10
56
+ protobuf==4.25.3
57
+ psutil==5.9.8
58
+ py-cpuinfo==9.0.0
59
+ pyarrow==15.0.1
60
+ pyasn1==0.5.1
61
+ pyasn1-modules==0.3.0
62
+ pybboxes==0.1.6
63
+ pydeck==0.8.1b0
64
+ Pygments==2.17.2
65
+ pyparsing==3.1.2
66
+ python-dateutil==2.9.0.post0
67
+ python-dotenv==1.0.1
68
+ python-magic==0.4.27
69
+ pytz==2024.1
70
+ PyWavelets==1.4.1
71
+ PyYAML==6.0.1
72
+ referencing==0.34.0
73
+ requests==2.31.0
74
+ requests-oauthlib==1.4.0
75
+ requests-toolbelt==1.0.0
76
+ rich==13.7.1
77
+ roboflow==1.1.24
78
+ rpds-py==0.18.0
79
+ rsa==4.9
80
+ s3transfer==0.10.1
81
+ sahi==0.11.15
82
+ scikit-image==0.21.0
83
+ scipy==1.10.1
84
+ seaborn==0.13.2
85
+ shapely==2.0.3
86
+ six==1.16.0
87
+ smmap==5.0.1
88
+ streamlit==1.32.2
89
+ sympy==1.12
90
+ tenacity==8.2.3
91
+ tensorboard==2.14.0
92
+ tensorboard-data-server==0.7.2
93
+ termcolor==2.4.0
94
+ terminaltables==3.1.10
95
+ thop==0.1.1.post2209072238
96
+ tifffile==2023.7.10
97
+ toml==0.10.2
98
+ toolz==0.12.1
99
+ torch==2.2.1
100
+ torchaudio==2.2.1
101
+ torchvision==0.17.1
102
+ tornado==6.4
103
+ tqdm==4.66.2
104
+ typing_extensions==4.10.0
105
+ tzdata==2024.1
106
+ ultralytics==8.1.29
107
+ urllib3==1.26.18
108
+ watchdog==4.0.0
109
+ Werkzeug==3.0.1
110
+ yolov5==7.0.13
111
+ zipp==3.18.1