Prathamesh1420 commited on
Commit
69a379e
·
verified ·
1 Parent(s): f5f2cf3

Upload 6 files

Browse files
Files changed (7) hide show
  1. .DS_Store +0 -0
  2. .gitattributes +1 -0
  3. best.pt +3 -0
  4. demo.mp4 +3 -0
  5. images.jpeg +0 -0
  6. requirements.txt +5 -0
  7. yolo_applicaiton.py +127 -0
.DS_Store ADDED
Binary file (6.15 kB). View file
 
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ demo.mp4 filter=lfs diff=lfs merge=lfs -text
best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ca30bc0b183d5932931b21c53b2e3f6ab8a773e26b0e737d36066da195507b05
3
+ size 6217113
demo.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e707d2ae1c912beb3e661a4c9f2b1587250e0abaa34bd524a7ceef0cdd26e93d
3
+ size 9563349
images.jpeg ADDED
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ pandas
2
+ matplotlib
3
+ streamlit
4
+ opencv-python
5
+ ultralytics
yolo_applicaiton.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import numpy as np
4
+ from ultralytics import YOLO
5
+ from PIL import Image
6
+ import os
7
+
8
+ # Set the title of the Streamlit app
9
+ st.title("YOLO Image and Video Processing")
10
+
11
+ # Allow users to upload images or videos
12
+ uploaded_file = st.file_uploader("Upload an image or video", type=["jpg", "jpeg", "png", "bmp", "mp4", "avi", "mov", "mkv"])
13
+
14
+ # Load YOLO model
15
+ try:
16
+ model = YOLO('/Users/A116351061/Documents/License_plate_detection/best.pt') # Replace with the path to your trained YOLO model
17
+ except Exception as e:
18
+ st.error(f"Error loading YOLO model: {e}")
19
+
20
+ def predict_and_save_image(path_test_car, output_image_path):
21
+ """
22
+ Predicts and saves the bounding boxes on the given test image using the trained YOLO model.
23
+
24
+ Parameters:
25
+ path_test_car (str): Path to the test image file.
26
+ output_image_path (str): Path to save the output image file.
27
+
28
+ Returns:
29
+ str: The path to the saved output image file.
30
+ """
31
+ try:
32
+ results = model.predict(path_test_car, device='cpu')
33
+ image = cv2.imread(path_test_car)
34
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
35
+ for result in results:
36
+ for box in result.boxes:
37
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
38
+ confidence = box.conf[0]
39
+ cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
40
+ cv2.putText(image, f'{confidence*100:.2f}%', (x1, y1 - 10),
41
+ cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
42
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
43
+ cv2.imwrite(output_image_path, image)
44
+ return output_image_path
45
+ except Exception as e:
46
+ st.error(f"Error processing image: {e}")
47
+ return None
48
+
49
+ def predict_and_plot_video(video_path, output_path):
50
+ """
51
+ Predicts and saves the bounding boxes on the given test video using the trained YOLO model.
52
+
53
+ Parameters:
54
+ video_path (str): Path to the test video file.
55
+ output_path (str): Path to save the output video file.
56
+
57
+ Returns:
58
+ str: The path to the saved output video file.
59
+ """
60
+ try:
61
+ cap = cv2.VideoCapture(video_path)
62
+ if not cap.isOpened():
63
+ st.error(f"Error opening video file: {video_path}")
64
+ return None
65
+ frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
66
+ frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
67
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
68
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
69
+ out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
70
+ while cap.isOpened():
71
+ ret, frame = cap.read()
72
+ if not ret:
73
+ break
74
+ rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
75
+ results = model.predict(rgb_frame, device='cpu')
76
+ for result in results:
77
+ for box in result.boxes:
78
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
79
+ confidence = box.conf[0]
80
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
81
+ cv2.putText(frame, f'{confidence*100:.2f}%', (x1, y1 - 10),
82
+ cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
83
+ out.write(frame)
84
+ cap.release()
85
+ out.release()
86
+ return output_path
87
+ except Exception as e:
88
+ st.error(f"Error processing video: {e}")
89
+ return None
90
+
91
+ def process_media(input_path, output_path):
92
+ """
93
+ Processes the uploaded media file (image or video) and returns the path to the saved output file.
94
+
95
+ Parameters:
96
+ input_path (str): Path to the input media file.
97
+ output_path (str): Path to save the output media file.
98
+
99
+ Returns:
100
+ str: The path to the saved output media file.
101
+ """
102
+ file_extension = os.path.splitext(input_path)[1].lower()
103
+ if file_extension in ['.mp4', '.avi', '.mov', '.mkv']:
104
+ return predict_and_plot_video(input_path, output_path)
105
+ elif file_extension in ['.jpg', '.jpeg', '.png', '.bmp']:
106
+ return predict_and_save_image(input_path, output_path)
107
+ else:
108
+ st.error(f"Unsupported file type: {file_extension}")
109
+ return None
110
+
111
+ if uploaded_file is not None:
112
+ input_path = os.path.join("temp", uploaded_file.name)
113
+ output_path = os.path.join("temp", f"output_{uploaded_file.name}")
114
+ try:
115
+ with open(input_path, "wb") as f:
116
+ f.write(uploaded_file.getbuffer())
117
+ st.write("Processing...")
118
+ result_path = process_media(input_path, output_path)
119
+ if result_path:
120
+ if input_path.endswith(('.mp4', '.avi', '.mov', '.mkv')):
121
+ video_file = open(result_path, 'rb')
122
+ video_bytes = video_file.read()
123
+ st.video(video_bytes)
124
+ else:
125
+ st.image(result_path)
126
+ except Exception as e:
127
+ st.error(f"Error uploading or processing file: {e}")