sneharaidu commited on
Commit
ad9abdd
1 Parent(s): 8b46c0b
Files changed (4) hide show
  1. app.py +99 -0
  2. logo.jpg +0 -0
  3. requirements.txt +111 -0
  4. yolov5s.pt +3 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import numpy as np
4
+ from yolov5 import YOLOv5
5
+ from sort.sort import Sort
6
+ import tempfile
7
+ import shutil
8
+ from moviepy.editor import VideoFileClip, concatenate_videoclips, ImageSequenceClip
9
+ import os
10
+
11
+ # Load the pre-trained model and initialize the SORT tracker
12
+ model_path = 'yolov5s.pt' # Ensure this path points to the model file
13
+ model = YOLOv5(model_path, device='cpu')
14
+ tracker = Sort()
15
+
16
+ def process_video(uploaded_file):
17
+ # Save the uploaded file to a temporary location
18
+ temp_file_path = "temp_video.mp4"
19
+ with open(temp_file_path, "wb") as temp_file:
20
+ temp_file.write(uploaded_file.getbuffer())
21
+
22
+ # Use moviepy to read the video file
23
+ video_clip = VideoFileClip(temp_file_path)
24
+ total_frames = int(video_clip.fps * video_clip.duration)
25
+ width, height = video_clip.size
26
+
27
+ # Temporary directory to save processed video frames
28
+ temp_dir = tempfile.mkdtemp()
29
+
30
+ unique_cars = set()
31
+ progress_bar = st.progress(0)
32
+
33
+ for frame_idx, frame in enumerate(video_clip.iter_frames()):
34
+ frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
35
+
36
+ progress_percentage = int((frame_idx + 1) / total_frames * 100)
37
+ progress_bar.progress(progress_percentage)
38
+
39
+ # Detection and tracking logic
40
+ results = model.predict(frame)
41
+ preds = results.pandas().xyxy[0]
42
+ detections = []
43
+
44
+ for index, row in preds.iterrows():
45
+ if row['name'] == 'car':
46
+ xmin, ymin, xmax, ymax, conf = row['xmin'], row['ymin'], row['xmax'], row['ymax'], row['confidence']
47
+ detections.append([xmin, ymin, xmax, ymax, conf])
48
+
49
+ if detections:
50
+ detections_np = np.array(detections)
51
+ trackers = tracker.update(detections_np)
52
+
53
+ for d in trackers:
54
+ unique_cars.add(int(d[4]))
55
+ xmin, ymin, xmax, ymax = map(int, d[:4])
56
+ cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (255, 0, 0), 2)
57
+ cv2.putText(frame, f'ID: {int(d[4])}', (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
58
+
59
+ cv2.putText(frame, f'Unique Cars: {len(unique_cars)}', (10, 35), cv2.FONT_HERSHEY_SIMPLEX, 1.25, (0, 255, 0), 2)
60
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert back to RGB for moviepy
61
+ cv2.imwrite(f"{temp_dir}/{frame_idx:04d}.jpg", frame)
62
+
63
+ frames_files = [os.path.join(temp_dir, f"{i:04d}.jpg") for i in range(total_frames)]
64
+ clip = ImageSequenceClip(frames_files, fps=video_clip.fps)
65
+ output_video_path = 'processed_video.mp4'
66
+ clip.write_videofile(output_video_path, codec='libx264') # Use libx264 codec for compatibility
67
+
68
+ # Remove temporary directory and temporary files
69
+ shutil.rmtree(temp_dir)
70
+
71
+ return output_video_path
72
+
73
+
74
+ def main():
75
+ # Initialize session state variables if they don't exist
76
+ if 'output_video_path' not in st.session_state:
77
+ st.session_state.output_video_path = None
78
+
79
+ st.sidebar.image("logo.jpg", use_column_width=True)
80
+ uploaded_file = st.sidebar.file_uploader("Upload a video", type=['mp4'])
81
+
82
+ st.title("Car Detection and Tracking")
83
+
84
+ if uploaded_file is not None:
85
+ # Process the video only if it hasn't been processed yet or a new file is uploaded
86
+ if st.session_state.output_video_path is None or st.session_state.uploaded_file_name != uploaded_file.name:
87
+ st.session_state.uploaded_file_name = uploaded_file.name
88
+ st.session_state.output_video_path = process_video(uploaded_file)
89
+
90
+ # Display the processed video
91
+ st.video(st.session_state.output_video_path)
92
+
93
+ # Provide a download link for the processed video
94
+ with open(st.session_state.output_video_path, "rb") as file:
95
+ st.download_button("Download Processed Video", file, file_name="processed_video.mp4")
96
+
97
+
98
+ if __name__ == "__main__":
99
+ 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
yolov5s.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8b3b748c1e592ddd8868022e8732fde20025197328490623cc16c6f24d0782ee
3
+ size 14808437