1ngsm0del commited on
Commit
fefd458
1 Parent(s): 5bb64b7

first commit

Browse files
RXBASE-600_00071-1014-68_NLMIMAGE10_5715ABFD.jpg ADDED
RXBASE-600_00074-7126-13_NLMIMAGE10_C003606B.jpg ADDED
RXNAV-600_13668-0095-90_RXNAVIMAGE10_D145E8EF.jpg ADDED
app.py ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import tempfile
4
+ from ultralytics import YOLOv10
5
+ import pandas as pd
6
+ from gradio import processing_utils
7
+
8
+ df = pd.read_csv('image_class.csv')
9
+ df = df[['name', 'class']]
10
+ df.drop_duplicates(inplace=True)
11
+ print(len(df))
12
+ df1 = pd.read_csv('image_class.csv')
13
+ df1 = df1[['name', 'class', 'im_file']]
14
+ df1['file_name'] = df1['im_file'].apply(lambda v: v.split('_')[-1].split('.')[0])
15
+ df1.drop(columns='im_file', inplace=True)
16
+ df1.drop_duplicates(inplace=True)
17
+ print(df1)
18
+ print(len(df1))
19
+
20
+ def yolov10_inference(image, video, image_size, conf_threshold, iou_threshold):
21
+ model = YOLOv10('./drug_yolov10.pt')
22
+ # model = YOLOv10('./pills_yolov10.pt')
23
+ if image:
24
+ results = model.predict(source=image, imgsz=image_size, conf=conf_threshold, iou=iou_threshold)
25
+ annotated_image = results[0].plot()
26
+ # Print the detected objects' information (class, coordinates, and probability)
27
+ box = results[0].boxes
28
+ cls = [int(c) for c in box.cls.tolist()]
29
+ cnf = [round(f,2) for f in box.conf.tolist()]
30
+ clcf = '\n'.join([f'Class:{cls[i]} , Confidence:{cnf[i]*100}%' for i in range(len(cls))]) #list(zip(cls,cnf))
31
+ name = '\n'.join([df[df['class']==n]['name'].item() for n in cls])
32
+ file_name = image.split('_')[-1].split('.')[0]
33
+ print(f'file name: {file_name}')
34
+ try:
35
+ drug_name = df1[df1['file_name']==file_name]['name'].item()
36
+ drug_class = df1[df1['file_name']==file_name]['class'].item()
37
+ drug_name = f'{drug_class}, {drug_name}'
38
+ print(drug_name)
39
+ except:
40
+ drug_name = 'No have data'
41
+ # print(cls)
42
+ # print(name)
43
+ # print(type(clcf))
44
+ # print("Object type:", box.cls)
45
+ # print("Coordinates:", box.xyxy)
46
+ # print("Probability:", box.conf)
47
+ # print('box.class data tyupe', type(box.cls.tolist()))
48
+ return annotated_image[:, :, ::-1], None, clcf, name, file_name, drug_name
49
+ else:
50
+ video_path = tempfile.mktemp(suffix=".webm")
51
+ with open(video_path, "wb") as f:
52
+ with open(video, "rb") as g:
53
+ f.write(g.read())
54
+
55
+ cap = cv2.VideoCapture(video_path)
56
+ fps = cap.get(cv2.CAP_PROP_FPS)
57
+ frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
58
+ frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
59
+
60
+ output_video_path = tempfile.mktemp(suffix=".webm")
61
+ out = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*'vp80'), fps, (frame_width, frame_height))
62
+
63
+ while cap.isOpened():
64
+ ret, frame = cap.read()
65
+ if not ret:
66
+ break
67
+
68
+ results = model.predict(source=frame, imgsz=image_size, conf=conf_threshold, iou=iou_threshold)
69
+ annotated_frame = results[0].plot()
70
+ out.write(annotated_frame)
71
+
72
+ cap.release()
73
+ out.release()
74
+
75
+ return None, output_video_path
76
+
77
+
78
+ def yolov10_inference_for_examples(image, image_size, conf_threshold, iou_threshold):
79
+ annotated_image, _, output_class, output_name = yolov10_inference(image, None, image_size, conf_threshold, iou_threshold)
80
+ return annotated_image#, None, output_class, output_name
81
+
82
+ def app():
83
+ with gr.Blocks():
84
+ with gr.Row():
85
+ with gr.Column():
86
+ # image = gr.Image(type="pil", label="Image", visible=True)
87
+ image = gr.Image(type="filepath", label="Image", visible=True)
88
+ video = gr.Video(label="Video", visible=False)
89
+ input_type = gr.Radio(
90
+ choices=["Image", "Video"],
91
+ value="Image",
92
+ label="Input Type",
93
+ )
94
+ file_name = gr.Textbox(label='File Name')
95
+ file_name.change(outputs=file_name)
96
+ drug_name = gr.Textbox(label='Drug Name')
97
+ drug_name.change(outputs=drug_name)
98
+
99
+ image_size = gr.Slider(
100
+ label="Image Size",
101
+ minimum=0,
102
+ maximum=1280,
103
+ step=10,
104
+ value=640,
105
+ )
106
+ conf_threshold = gr.Slider(
107
+ label="Confidence Threshold",
108
+ minimum=0.0,
109
+ maximum=1.0,
110
+ step=0.05,
111
+ value=0.25,
112
+ )
113
+ iou_threshold = gr.Slider(
114
+ label="IOU Threshold",
115
+ minimum=0,
116
+ maximum=1,
117
+ step=0.1,
118
+ value=0.6,
119
+ )
120
+ yolov10_infer = gr.Button(value="Detect Objects")
121
+
122
+ with gr.Column():
123
+ output_image = gr.Image(type="numpy", label="Annotated Image", visible=True)
124
+ output_video = gr.Video(label="Annotated Video", visible=False)
125
+ output_name = gr.Textbox(label='Predicted Drug Name')
126
+ output_name.change(outputs=output_name)
127
+ output_class = gr.Textbox(label='Predicted Class')
128
+ output_class.change(outputs=output_class)
129
+
130
+ def update_visibility(input_type):
131
+ image = gr.update(visible=True) if input_type == "Image" else gr.update(visible=False)
132
+ video = gr.update(visible=False) if input_type == "Image" else gr.update(visible=True)
133
+ output_image = gr.update(visible=True) if input_type == "Image" else gr.update(visible=False)
134
+ output_video = gr.update(visible=False) if input_type == "Image" else gr.update(visible=True)
135
+ print(f'updated image: {image}')
136
+
137
+ return image, video, output_image, output_video
138
+
139
+ input_type.change(
140
+ fn=update_visibility,
141
+ inputs=[input_type],
142
+ outputs=[image, video, output_image, output_video],
143
+ )
144
+
145
+ def run_inference(image, video, image_size, conf_threshold, iou_threshold, input_type):
146
+ if input_type == "Image":
147
+ return yolov10_inference(image, None, image_size, conf_threshold, iou_threshold)
148
+ else:
149
+ return yolov10_inference(None, video, image_size, conf_threshold, iou_threshold)
150
+
151
+
152
+ yolov10_infer.click(
153
+ fn=run_inference,
154
+ inputs=[image, video, image_size, conf_threshold, iou_threshold, input_type],
155
+ outputs=[output_image, output_video, output_class, output_name, file_name, drug_name],
156
+ )
157
+
158
+ gr.Examples(
159
+ examples = [
160
+ ['./RXBASE-600_00071-1014-68_NLMIMAGE10_5715ABFD.jpg', 280, 0.2, 0.6],
161
+ ['./RXNAV-600_13668-0095-90_RXNAVIMAGE10_D145E8EF.jpg', 640, 0.2, 0.7],
162
+ ['./RXBASE-600_00074-7126-13_NLMIMAGE10_C003606B.jpg', 640, 0.2, 0.8],
163
+ ],
164
+ fn=yolov10_inference_for_examples,
165
+ inputs=[
166
+ image,
167
+ image_size,
168
+ conf_threshold,
169
+ iou_threshold,
170
+ ],
171
+ outputs=[output_image],
172
+ cache_examples='lazy',
173
+ )
174
+
175
+ gradio_app = gr.Blocks()
176
+ with gradio_app:
177
+ gr.HTML(
178
+ """
179
+ <h1 style='text-align: center'>
180
+ YOLOv10: Real-Time End-to-End Object Detection
181
+ </h1>
182
+ """)
183
+ gr.HTML(
184
+ """
185
+ <h3 style='text-align: center'>
186
+ <a href='https://arxiv.org/abs/2405.14458' target='_blank'>arXiv</a> | <a href='https://github.com/THU-MIG/yolov10' target='_blank'>github</a>
187
+ </h3>
188
+ """)
189
+ with gr.Row():
190
+ with gr.Column():
191
+ app()
192
+ if __name__ == '__main__':
193
+ gradio_app.launch()
drug_yolov10.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d953e3db8e56519197a3e6d74d2b078226f7f1cf1de07064631003acb4a493f3
3
+ size 33211887
gradio_cached_examples/23/Annotated Image/ba594e145eaf95ab5434/image.webp ADDED
gradio_cached_examples/23/indices.csv ADDED
@@ -0,0 +1 @@
 
 
1
+ 0
gradio_cached_examples/23/log.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Annotated Image,flag,username,timestamp
2
+ "{""path"": ""gradio_cached_examples/23/Annotated Image/ba594e145eaf95ab5434/image.webp"", ""url"": ""/file=/tmp/gradio/1d7004412f6a7ce1df9e4d03b3144ae1bbc791ff/image.webp"", ""size"": null, ""orig_name"": ""image.webp"", ""mime_type"": null, ""is_stream"": false, ""meta"": {""_type"": ""gradio.FileData""}}",,,2024-06-07 17:57:56.712008
gradio_cached_examples/24/Annotated Image/218b53a36785220fa9c1/image.webp ADDED
gradio_cached_examples/24/indices.csv ADDED
@@ -0,0 +1 @@
 
 
1
+ 0
gradio_cached_examples/24/log.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Annotated Image,flag,username,timestamp
2
+ "{""path"": ""gradio_cached_examples/24/Annotated Image/218b53a36785220fa9c1/image.webp"", ""url"": ""/file=/tmp/gradio/1d7004412f6a7ce1df9e4d03b3144ae1bbc791ff/image.webp"", ""size"": null, ""orig_name"": ""image.webp"", ""mime_type"": null, ""is_stream"": false, ""meta"": {""_type"": ""gradio.FileData""}}",,,2024-06-07 21:20:45.740938
image_class.csv ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio==4.32.1
2
+ opencv_python==4.8.1.78
3
+ opencv_python_headless==4.8.0.74
4
+ git+https://github.com/THU-MIG/yolov10
test.ipynb ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "import pandas as pd"
10
+ ]
11
+ },
12
+ {
13
+ "cell_type": "code",
14
+ "execution_count": 25,
15
+ "metadata": {},
16
+ "outputs": [
17
+ {
18
+ "name": "stdout",
19
+ "output_type": "stream",
20
+ "text": [
21
+ "2111\n",
22
+ " name class file_name\n",
23
+ "0 Loperamide Hydrochloride 2 MG Oral Capsule 1052 26211358.jpg\n",
24
+ "1 Minocycline 50 MG Oral Capsule 1137 36231B28.jpg\n",
25
+ "2 Nortriptyline 10 MG Oral Capsule 1193 24231228.jpg\n",
26
+ "3 Nortriptyline 25 MG Oral Capsule 1194 20231018.jpg\n",
27
+ "4 Nortriptyline 50 MG Oral Capsule 1195 2D2316D8.jpg\n",
28
+ "... ... ... ...\n",
29
+ "4327 Enalapril Maleate 2.5 MG Oral Tablet 708 DC4D6E2B.jpg\n",
30
+ "4328 quinapril 5 MG Oral Tablet 1844 EA507553.jpg\n",
31
+ "4329 trospium chloride 20 MG Oral Tablet 1959 3C519E2C.jpg\n",
32
+ "4330 metaxalone 400 MG Oral Tablet 1749 C6506353.jpg\n",
33
+ "4331 naratriptan 2.5 MG Oral Tablet 1775 66453369.jpg\n",
34
+ "\n",
35
+ "[4332 rows x 3 columns]\n",
36
+ "4332\n"
37
+ ]
38
+ }
39
+ ],
40
+ "source": [
41
+ "df = pd.read_csv('image_class.csv')\n",
42
+ "df = df[['name', 'class']]\n",
43
+ "df.drop_duplicates(inplace=True)\n",
44
+ "print(len(df))\n",
45
+ "df1 = pd.read_csv('image_class.csv')\n",
46
+ "df1 = df1[['name', 'class', 'im_file']]\n",
47
+ "df1['file_name'] = df1['im_file'].apply(lambda v: v.split('_')[-1])\n",
48
+ "df1.drop(columns='im_file', inplace=True)\n",
49
+ "df1.drop_duplicates(inplace=True)\n",
50
+ "print(df1)\n",
51
+ "print(len(df1))"
52
+ ]
53
+ },
54
+ {
55
+ "cell_type": "code",
56
+ "execution_count": 26,
57
+ "metadata": {},
58
+ "outputs": [
59
+ {
60
+ "data": {
61
+ "text/html": [
62
+ "<div>\n",
63
+ "<style scoped>\n",
64
+ " .dataframe tbody tr th:only-of-type {\n",
65
+ " vertical-align: middle;\n",
66
+ " }\n",
67
+ "\n",
68
+ " .dataframe tbody tr th {\n",
69
+ " vertical-align: top;\n",
70
+ " }\n",
71
+ "\n",
72
+ " .dataframe thead th {\n",
73
+ " text-align: right;\n",
74
+ " }\n",
75
+ "</style>\n",
76
+ "<table border=\"1\" class=\"dataframe\">\n",
77
+ " <thead>\n",
78
+ " <tr style=\"text-align: right;\">\n",
79
+ " <th></th>\n",
80
+ " <th>name</th>\n",
81
+ " <th>class</th>\n",
82
+ " <th>file_name</th>\n",
83
+ " </tr>\n",
84
+ " </thead>\n",
85
+ " <tbody>\n",
86
+ " <tr>\n",
87
+ " <th>213</th>\n",
88
+ " <td>pregabalin 75 MG Oral Capsule [Lyrica]</td>\n",
89
+ " <td>1831</td>\n",
90
+ " <td>5715ABFD.jpg</td>\n",
91
+ " </tr>\n",
92
+ " </tbody>\n",
93
+ "</table>\n",
94
+ "</div>"
95
+ ],
96
+ "text/plain": [
97
+ " name class file_name\n",
98
+ "213 pregabalin 75 MG Oral Capsule [Lyrica] 1831 5715ABFD.jpg"
99
+ ]
100
+ },
101
+ "execution_count": 26,
102
+ "metadata": {},
103
+ "output_type": "execute_result"
104
+ }
105
+ ],
106
+ "source": [
107
+ "# df1[df1.file_name.str.endswith('26211358.jpg')]\n",
108
+ "df1[df1.file_name.str.endswith('5715ABFD.jpg')]"
109
+ ]
110
+ },
111
+ {
112
+ "cell_type": "code",
113
+ "execution_count": 8,
114
+ "metadata": {},
115
+ "outputs": [
116
+ {
117
+ "ename": "TypeError",
118
+ "evalue": "'StringMethods' object is not callable",
119
+ "output_type": "error",
120
+ "traceback": [
121
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
122
+ "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
123
+ "Cell \u001b[0;32mIn[8], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# file_name = '00071-1014-68_NLMIMAGE10_5715ABFD.jpeg'\u001b[39;00m\n\u001b[1;32m 2\u001b[0m file_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m23155-0055-19_NLMIMAGE10_66453369.jpg\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m----> 3\u001b[0m df1[\u001b[43mdf1\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mfile_name\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstr\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39msplit(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m_\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;241m1\u001b[39m)[\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m]\u001b[38;5;241m==\u001b[39mfile_name]\n",
124
+ "\u001b[0;31mTypeError\u001b[0m: 'StringMethods' object is not callable"
125
+ ]
126
+ }
127
+ ],
128
+ "source": [
129
+ "# file_name = '00071-1014-68_NLMIMAGE10_5715ABFD.jpeg'\n",
130
+ "file_name = '23155-0055-19_NLMIMAGE10_66453369.jpg'\n",
131
+ "df1[df1['file_name'].str().split('_',1)[-1]==file_name]"
132
+ ]
133
+ },
134
+ {
135
+ "cell_type": "code",
136
+ "execution_count": 15,
137
+ "metadata": {},
138
+ "outputs": [
139
+ {
140
+ "data": {
141
+ "text/plain": [
142
+ "0 [00093-0311-01, RXNAVIMAGE10, 26211358.jpg]\n",
143
+ "1 [00093-3165-01, RXNAVIMAGE10, 36231B28.jpg]\n",
144
+ "2 [00093-0810-01, RXNAVIMAGE10, 24231228.jpg]\n",
145
+ "3 [00093-0811-01, RXNAVIMAGE10, 20231018.jpg]\n",
146
+ "4 [00093-0812-01, RXNAVIMAGE10, 2D2316D8.jpg]\n",
147
+ " ... \n",
148
+ "8629 [16714-0442-01, NLMIMAGE10, DC4D6E2B.jpg]\n",
149
+ "8630 [31722-0267-90, NLMIMAGE10, EA507553.jpg]\n",
150
+ "8631 [00574-0145-60, NLMIMAGE10, 3C519E2C.jpg]\n",
151
+ "8632 [64720-0126-10, NLMIMAGE10, C6506353.jpg]\n",
152
+ "8633 [23155-0055-19, NLMIMAGE10, 66453369.jpg]\n",
153
+ "Name: file_name, Length: 8634, dtype: object"
154
+ ]
155
+ },
156
+ "execution_count": 15,
157
+ "metadata": {},
158
+ "output_type": "execute_result"
159
+ }
160
+ ],
161
+ "source": [
162
+ "df1.file_name.str.split('_')"
163
+ ]
164
+ },
165
+ {
166
+ "cell_type": "code",
167
+ "execution_count": null,
168
+ "metadata": {},
169
+ "outputs": [],
170
+ "source": []
171
+ }
172
+ ],
173
+ "metadata": {
174
+ "kernelspec": {
175
+ "display_name": "yolov9",
176
+ "language": "python",
177
+ "name": "python3"
178
+ },
179
+ "language_info": {
180
+ "codemirror_mode": {
181
+ "name": "ipython",
182
+ "version": 3
183
+ },
184
+ "file_extension": ".py",
185
+ "mimetype": "text/x-python",
186
+ "name": "python",
187
+ "nbconvert_exporter": "python",
188
+ "pygments_lexer": "ipython3",
189
+ "version": "3.9.19"
190
+ }
191
+ },
192
+ "nbformat": 4,
193
+ "nbformat_minor": 2
194
+ }