ethanrom commited on
Commit
5b5b64e
·
1 Parent(s): d30899c
Files changed (8) hide show
  1. 1.jpg +0 -0
  2. app.py +172 -0
  3. image.jpg +0 -0
  4. markup.py +72 -0
  5. object_detection_script.py +40 -0
  6. requirements.txt +38 -0
  7. yolov8n.onnx +3 -0
  8. yolov8n.pt +3 -0
1.jpg ADDED
app.py ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ from PIL import Image
4
+ from io import BytesIO
5
+ from ultralytics import YOLO
6
+ import numpy as np
7
+ from streamlit_option_menu import option_menu
8
+ from markup import real_estate_app, real_estate_app_hf
9
+
10
+ model = YOLO('yolov8n.onnx')
11
+
12
+ PASSWORD = 'Ethan101'
13
+
14
+ def authenticate(password):
15
+ return password == PASSWORD
16
+
17
+ def tab1():
18
+ st.header("Human and Vehicle Recognition Demo")
19
+ col1, col2 = st.columns([1, 2])
20
+ with col1:
21
+ st.image("image.jpg", use_column_width=True)
22
+ with col2:
23
+ st.markdown(real_estate_app(), unsafe_allow_html=True)
24
+ st.markdown(real_estate_app_hf(),unsafe_allow_html=True)
25
+
26
+
27
+ github_link = '[<img src="https://badgen.net/badge/icon/github?icon=github&label">](https://github.com/ethanrom)'
28
+ #huggingface_link = '[<img src="https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue">](https://huggingface.co/ethanrom)'
29
+
30
+ st.write(github_link + '&nbsp;&nbsp;&nbsp;', unsafe_allow_html=True)
31
+
32
+ def tab2():
33
+ st.header("Test Detection Algorithm")
34
+ uploaded_file = st.file_uploader('Choose an image', type=['jpg', 'jpeg', 'png'])
35
+ if uploaded_file is not None:
36
+ image = Image.open(uploaded_file)
37
+ image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
38
+
39
+ col1, col2 = st.columns([2,1])
40
+ with col2:
41
+ iou_threshold = st.slider('IoU Threshold', min_value=0.0, max_value=1.0, value=0.7)
42
+ conf_threshold = st.slider('Confidence Threshold', min_value=0.0, max_value=1.0, value=0.65)
43
+ show_labels = st.checkbox('Show Labels', value=False)
44
+ show_conf = st.checkbox('Show Confidence Scores', value=False)
45
+ boxes = st.checkbox('Show Boxes', value=True)
46
+
47
+ with col1:
48
+ st.image(image, caption='Input Image', use_column_width=True)
49
+
50
+ if st.button('Apply and Predict'):
51
+ results = model(
52
+ image_cv,
53
+ classes=[0,2,7,3,5],
54
+ iou=iou_threshold,
55
+ conf=conf_threshold,
56
+ show_labels=show_labels,
57
+ show_conf=show_conf,
58
+ boxes=boxes,
59
+ )
60
+
61
+ annotated_frame = results[0].plot()
62
+ annotated_image = Image.fromarray(cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB))
63
+ st.image(annotated_image, caption='Annotated Image', use_column_width=True)
64
+
65
+
66
+
67
+ def tab3():
68
+ st.header("Configure and save script")
69
+ st.write("Please send me a DM to get the password")
70
+
71
+ password_input = st.text_input('Enter Password', type='password')
72
+ if authenticate(password_input):
73
+
74
+ source_folder = st.text_input('Source Folder Location')
75
+ results_folder = st.text_input('Destination Folder Location for Object Detection Results')
76
+
77
+ script = f"""
78
+ import os
79
+ import cv2
80
+ from ultralytics import YOLO
81
+ from tqdm import tqdm
82
+
83
+ model = YOLO('yolov8n.pt')
84
+
85
+ def detect_cars_humans(image_path):
86
+ image_cv = cv2.imread(image_path)
87
+
88
+ # Perform object detection
89
+ results = model(
90
+ image_cv,
91
+ classes=[0, 2, 7, 3, 5],
92
+ iou=0.7,
93
+ conf=0.65,
94
+ show_labels=False,
95
+ show_conf=False,
96
+ boxes=True
97
+ )
98
+
99
+ if len(results[0].boxes.xyxy) == 0:
100
+ return
101
+
102
+ # Create the destination folder if it doesn't exist
103
+ os.makedirs(r"{results_folder}", exist_ok=True)
104
+
105
+ # Save the annotated image in the results folder
106
+ annotated_image_path = os.path.join(r"{results_folder}", os.path.basename(image_path))
107
+ cv2.imwrite(annotated_image_path, results[0].plot())
108
+
109
+ source_folder = r"{source_folder}"
110
+ image_files = [f for f in os.listdir(source_folder) if f.endswith(".png") or f.endswith(".jpg")]
111
+
112
+ with tqdm(total=len(image_files), desc='Processing Images') as pbar:
113
+ for filename in image_files:
114
+ image_path = os.path.join(source_folder, filename)
115
+ detect_cars_humans(image_path)
116
+ pbar.update(1)
117
+ """
118
+ st.code(script, language='python')
119
+
120
+ if st.button('Download Script'):
121
+ script_filename = 'object_detection_script.py'
122
+ with open(script_filename, 'w') as file:
123
+ file.write(script)
124
+ st.download_button(
125
+ label='Download Script',
126
+ data=script_filename,
127
+ file_name=script_filename
128
+ )
129
+
130
+ if st.button('Download Requirements'):
131
+ requirements_filename = 'requirements.txt'
132
+ with open(requirements_filename, 'r') as file:
133
+ requirements_content = file.read()
134
+ st.download_button(
135
+ label='Download Requirements',
136
+ data=requirements_content,
137
+ file_name=requirements_filename
138
+ )
139
+
140
+ st.subheader("Instructions:")
141
+ st.write("1. Set the source folder and destination folder locations.")
142
+ st.write("2. Click on the 'Download Script' button to download the object detection script.")
143
+ st.write("3. Click on the 'Download Requirements' button to download the requirements.txt file.")
144
+ st.write("4. Open a terminal or command prompt and navigate to the project directory.")
145
+ st.write("5. Run the following command to install the required packages:")
146
+ st.code("pip install -r requirements.txt")
147
+ st.write("6. Finally, run the object detection script using the following command:")
148
+ st.code("python object_detection_script.py")
149
+
150
+ else:
151
+ # Password is incorrect, show an error message
152
+ st.error('Invalid password. Access denied.')
153
+
154
+ def main():
155
+ st.set_page_config(page_title="Human and vehicle recognition", page_icon=":memo:", layout="wide")
156
+ tabs = ["Intro", "Test", "Download Script"]
157
+
158
+ with st.sidebar:
159
+
160
+ current_tab = option_menu("Select a Tab", tabs, menu_icon="cast")
161
+
162
+ tab_functions = {
163
+ "Intro": tab1,
164
+ "Test": tab2,
165
+ "Download Script": tab3,
166
+ }
167
+
168
+ if current_tab in tab_functions:
169
+ tab_functions[current_tab]()
170
+
171
+ if __name__ == "__main__":
172
+ main()
image.jpg ADDED
markup.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def real_estate_app():
2
+ return """
3
+ <h3 style='text-align: center;'>Introduction</h3>
4
+
5
+ <p>This app allows you to upload an image and remove its background using a custom trained model.</p>
6
+
7
+ <h4>Information:</h4>
8
+ <ul>
9
+ <li><b>Test Detection Algorithm:</b> In this demo you can easily test the accuracy of the detection algorithm by uploading photos with cars and humans </li>
10
+ <li><b>Customize and Download Script:</b> You can download the personlized script once done.</li>
11
+ </ul>
12
+ </div>
13
+ """
14
+
15
+ def real_estate_app_hf():
16
+ return """
17
+ <div style='text-align: left;'>
18
+ <h3 style='text-align: center;'>About this Demo</h3>
19
+ <p>In this demo, the two requested features have been split into two separate tabs for better individual evaluation. They can be combined into one in the final submission.</p>
20
+ <br>
21
+ <h4>How to use:</h4>
22
+ <ul>
23
+ <li><b>Testing:</b> Simply upload any image with humans and cars, click apply and predict button</li>
24
+ <li><b>Correct Perspective:</b> You can set the destination folder, source folder and other settings to your choice via the GUI and click donwload button to download the finished script.</li>
25
+ </ul>
26
+ <br>
27
+ </div>
28
+ """
29
+
30
+ def sliders_intro():
31
+ return """
32
+ <p>Newly added sliders which will appear after an image is uploaded serve as interactive tools to adjust various aspects of the uploaded image. Each slider corresponds to a specific color channel (red, green, blue) or a curve adjustment.
33
+ By using these sliders, users can fine-tune the color levels and apply curve modifications to achieve the desired visual effect.</p>
34
+ <p>For the RGB Adjustments section, users can use the Red, Green, and Blue sliders to set the minimum and maximum values for each color channel.
35
+ By adjusting these values, users can enhance or reduce the intensity of each color channel individually, allowing for precise color adjustments.</p>
36
+ <p>In the Curves Adjustment section, users can utilize the Red Curve, Green Curve, and Blue Curve sliders to control the brightness of the respective color channels.
37
+ By moving these sliders, users can create custom curves, influencing the overall tone and contrast of the image.</p>
38
+ <p>The Masking section offers the Threshold slider, which determines the cutoff point for the transparency mask.
39
+ Users can adjust the threshold value to define the boundary between foreground and background elements. This feature enables users to isolate objects by selectively applying transparency to specific areas of the image.</p>
40
+ """
41
+
42
+ def perspective_intro():
43
+ return """
44
+ there are two different perspective correction methods you can chose from, the difference is how they determine the transformation matrix used for warping the image.
45
+
46
+ In the Four-Point Perspective Correction, the method uses a four-point perspective transform. It first detects lines in the image using the HoughLinesP function, and then calculates the endpoints of these lines.
47
+ If enough endpoints are found (at least 4), a convex hull is created based on these endpoints. From the convex hull, a four-sided polygon is obtained, representing the region of interest.
48
+ The width and height of this polygon are determined, and a destination set of points is defined to which the polygon will be mapped.
49
+ Finally, a perspective transformation matrix is computed using getPerspectiveTransform function, and the image is warped accordingly.
50
+
51
+ In the Convex Hull Homography Perspective Correction, a similar process is followed, but instead of using a four-point perspective transform, it uses a homography transform.
52
+ After obtaining the endpoints, a convex hull is created, and a four-sided polygon is extracted from it. The width and height of this polygon are calculated, and a destination set of points is defined.
53
+ But instead of using getPerspectiveTransform, the findHomography function is used to compute the homography matrix.
54
+ This matrix defines the transformation between the source polygon and the destination polygon, and the image is warped using the warpPerspective function.
55
+
56
+ The parameters threshold_value, min_line_length, and max_line_gap in both methods control the detection of lines in the image.
57
+ These parameters affect the number and quality of lines detected, which in turn can impact the accuracy of the perspective correction.
58
+ Adjusting these values allows fine-tuning the perspective correction process based on the specific characteristics of the input image.
59
+ However, it is important to note that changing these values requires some experimentation to achieve the desired results, and it is recommended to find the optimal values through trial and error.
60
+ """
61
+
62
+ def manual_bg_intro():
63
+ return """
64
+ Click on the canvas and select four or more points, starting from any one corner of the slab and proceeding in order in any direction. The background will be removed using the selected points.
65
+ """
66
+
67
+ def segement_intro():
68
+ return """
69
+ Uses a custom trained model on multiple variations of the provided images. Accucary will increase if more images are provided and model is fine tuned.
70
+ fine tuning the model for new images is a straight forward process, necessary documentaion and guidlines will be provided.
71
+ Adjust the confidence score and IoU sliders to refine the selected area.
72
+ """
object_detection_script.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import cv2
4
+ from ultralytics import YOLO
5
+ from tqdm import tqdm
6
+
7
+ model = YOLO('yolov8n.pt')
8
+
9
+ def detect_cars_humans(image_path):
10
+ image_cv = cv2.imread(image_path)
11
+
12
+ # Perform object detection
13
+ results = model(
14
+ image_cv,
15
+ classes=[0, 2, 7, 3, 5],
16
+ iou=0.7,
17
+ conf=0.65,
18
+ show_labels=False,
19
+ show_conf=False,
20
+ boxes=True
21
+ )
22
+
23
+ if len(results[0].boxes.xyxy) == 0:
24
+ return
25
+
26
+ # Create the destination folder if it doesn't exist
27
+ os.makedirs(r"D:\ascii\car_person\testing\Test_Purpose\Test1_results", exist_ok=True)
28
+
29
+ # Save the annotated image in the results folder
30
+ annotated_image_path = os.path.join(r"D:\ascii\car_person\testing\Test_Purpose\Test1_results", os.path.basename(image_path))
31
+ cv2.imwrite(annotated_image_path, results[0].plot())
32
+
33
+ source_folder = r"D:\ascii\car_person\testing\Test_Purpose\Test1"
34
+ image_files = [f for f in os.listdir(source_folder) if f.endswith(".png") or f.endswith(".jpg")]
35
+
36
+ with tqdm(total=len(image_files), desc='Processing Images') as pbar:
37
+ for filename in image_files:
38
+ image_path = os.path.join(source_folder, filename)
39
+ detect_cars_humans(image_path)
40
+ pbar.update(1)
requirements.txt ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # pip install -r requirements.txt
2
+ ultralytics
3
+ streamlit
4
+ streamlit_option_menu
5
+ onnx
6
+ onnxruntime
7
+ tqdm
8
+
9
+ # base ----------------------------------------
10
+ matplotlib>=3.2.2
11
+ numpy>=1.18.5
12
+ opencv-python
13
+ opencv-python-headless>=4.1.2
14
+ Pillow
15
+ PyYAML>=5.3.1
16
+ scipy>=1.4.1
17
+ torch==1.7.1
18
+ torchvision>=0.8.1
19
+ tqdm>=4.41.0
20
+ click==8
21
+
22
+ # logging -------------------------------------
23
+ tensorboard>=2.4.1
24
+ # wandb
25
+
26
+ # plotting ------------------------------------
27
+ seaborn>=0.11.0
28
+ pandas
29
+
30
+ # export --------------------------------------
31
+ # coremltools>=4.1
32
+ # onnx>=1.8.1
33
+ # scikit-learn==0.19.2 # for coreml quantization
34
+
35
+ # extras --------------------------------------
36
+ thop # FLOPS computation
37
+ pycocotools>=2.0 # COCO mAP
38
+ scikit-learn
yolov8n.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e97df4e336ab724b22b4513ff39040a08770428e54628abd31b642e7babf75e5
3
+ size 12823491
yolov8n.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:31e20dde3def09e2cf938c7be6fe23d9150bbbe503982af13345706515f2ef95
3
+ size 6534387