Abhiyoshi commited on
Commit
74d0bb5
·
verified ·
1 Parent(s): 451776f

Upload 16 files

Browse files
Files changed (16) hide show
  1. .gitattributes +36 -35
  2. .gitignore +3 -0
  3. README.md +75 -13
  4. app.py +198 -0
  5. image_0.jpg +0 -0
  6. image_1.jpg +0 -0
  7. image_2.jpg +0 -0
  8. image_3.jpg +0 -0
  9. image_4.jpg +3 -0
  10. image_5.jpg +0 -0
  11. lp1.jpg +0 -0
  12. lp2.jpg +0 -0
  13. nlp1.jpg +0 -0
  14. nlp2.jpg +0 -0
  15. obj.pt +3 -0
  16. requirements.txt +51 -0
.gitattributes CHANGED
@@ -1,35 +1,36 @@
1
- *.7z filter=lfs diff=lfs merge=lfs -text
2
- *.arrow filter=lfs diff=lfs merge=lfs -text
3
- *.bin filter=lfs diff=lfs merge=lfs -text
4
- *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
- *.ftz filter=lfs diff=lfs merge=lfs -text
7
- *.gz filter=lfs diff=lfs merge=lfs -text
8
- *.h5 filter=lfs diff=lfs merge=lfs -text
9
- *.joblib filter=lfs diff=lfs merge=lfs -text
10
- *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
- *.model filter=lfs diff=lfs merge=lfs -text
13
- *.msgpack filter=lfs diff=lfs merge=lfs -text
14
- *.npy filter=lfs diff=lfs merge=lfs -text
15
- *.npz filter=lfs diff=lfs merge=lfs -text
16
- *.onnx filter=lfs diff=lfs merge=lfs -text
17
- *.ot filter=lfs diff=lfs merge=lfs -text
18
- *.parquet filter=lfs diff=lfs merge=lfs -text
19
- *.pb filter=lfs diff=lfs merge=lfs -text
20
- *.pickle filter=lfs diff=lfs merge=lfs -text
21
- *.pkl filter=lfs diff=lfs merge=lfs -text
22
- *.pt filter=lfs diff=lfs merge=lfs -text
23
- *.pth filter=lfs diff=lfs merge=lfs -text
24
- *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
- saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
- *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
- *.tflite filter=lfs diff=lfs merge=lfs -text
30
- *.tgz filter=lfs diff=lfs merge=lfs -text
31
- *.wasm filter=lfs diff=lfs merge=lfs -text
32
- *.xz 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
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz 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
+ image_4.jpg filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ flagged/
2
+ *.pt
3
+ gradio_cached_examples/
README.md CHANGED
@@ -1,13 +1,75 @@
1
- ---
2
- title: Lepdect
3
- emoji: 📊
4
- colorFrom: pink
5
- colorTo: indigo
6
- sdk: gradio
7
- sdk_version: 5.23.0
8
- app_file: app.py
9
- pinned: false
10
- short_description: leprosy detection
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Leprosy Detection
3
+ emoji:
4
+ colorFrom: red
5
+ colorTo: gray
6
+ sdk: gradio
7
+ sdk_version: 3.44.4
8
+ app_file: app.py
9
+ pinned: false
10
+ license: cc-by-4.0
11
+ ---
12
+
13
+ # Leprosy Detection App
14
+
15
+ Leprosy Gradio Detection web application. In this application, I have used a fine tuned YOLOv5s model to detect Leprosy samples based on images. The application uses gradio as the platform and can also be used in the [Huggingface online hosting application](https://huggingface.co/spaces/Arekku21/Leprosy-Detection).
16
+
17
+ ## Overview
18
+ [Leprosy](https://en.wikipedia.org/wiki/Leprosy), also known as Hansen's disease, is a chronic infectious disease that primarily affects the skin and peripheral nerves. The model used is a YOLO model from [Ultralytics](https://github.com/ultralytics/yolov5) with their version YOLOv5.
19
+
20
+ ### Features
21
+
22
+ - Upload an image to the app.
23
+ - Utilizes a fine-tuned YOLOv5s model for leprosy detection.
24
+ - Detect and label leprosy regions in the uploaded image.
25
+
26
+ ## Prerequisites
27
+
28
+ Before running the application, make sure you have the following prerequisites installed on your system:
29
+
30
+ - Python 3.x
31
+ - Git
32
+ - Gradio
33
+ - Pip package manager
34
+ - Conda Virtual environment (optional but recommended)
35
+
36
+ ### Installation Steps and Running the app
37
+
38
+ To install the required Python libraries, navigate to the project directory and run the following command:
39
+
40
+ #### Step 1 Clone the repository
41
+ ```
42
+ # Make sure you have git-lfs installed (https://git-lfs.com)
43
+ git lfs install
44
+ git clone https://huggingface.co/spaces/Arekku21/Leprosy-Detection
45
+
46
+ # if you want to clone without large files – just their pointers
47
+ # prepend your git clone with the following env var:
48
+ GIT_LFS_SKIP_SMUDGE=1
49
+ ```
50
+
51
+ #### Step 2 Install requirements
52
+ ```
53
+ #navigate to your cloned repository and location of requirmenents.txt
54
+ pip install -r requirements.txt
55
+ ```
56
+
57
+ #### Step 3 Run the app
58
+ ```
59
+ #ensure that you are using the right environment or have all the requirements installed
60
+ #ensure that you are navigated to the cloned repository
61
+ python app.py
62
+ ```
63
+
64
+ #### Step 4 Using the app
65
+ Your terminal should look like this and follow the local host URL link to use the application.
66
+ ```
67
+ Downloading: "https://github.com/ultralytics/yolov5/zipball/master" to C:\Users\master.zip
68
+
69
+ YOLOv5 2023-9-28 Python-3.9.18 torch-2.0.1+cpu CPU
70
+
71
+ Fusing layers...
72
+ Model summary: 157 layers, 7015519 parameters, 0 gradients, 15.8 GFLOPs
73
+ Adding AutoShape...
74
+ Running on local URL: http://127.0.0.1:7860
75
+ ```
app.py ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import requests
4
+ import os
5
+
6
+ import torch
7
+ from PIL import Image
8
+ import tempfile
9
+
10
+ import numpy as np
11
+
12
+ import math
13
+
14
+
15
+ file_urls = [
16
+ 'https://www.dropbox.com/scl/fi/onrg1u9tqegh64nsfmxgr/lp2.jpg?rlkey=2vgw5n6abqmyismg16mdd1v3n&dl=1',
17
+ 'https://www.dropbox.com/scl/fi/xq103ic7ovuuei3l9e8jf/lp1.jpg?rlkey=g7d9khyyc6wplv0ljd4mcha60&dl=1',
18
+ 'https://www.dropbox.com/scl/fi/fagkh3gnio2pefdje7fb9/Non_Leprosy_210823_86_jpg.rf.5bb80a7704ecc6c8615574cad5d074c5.jpg?rlkey=ks8afue5gsx5jqvxj3u9mbjmg&dl=1',
19
+ 'https://www.dropbox.com/scl/fi/gh4zotrzic5y00ok3crje/Non_Leprosy_210823_46_jpg.rf.76c20cb340114a98618ade07c3e6b413.jpg?rlkey=pxdjlhxipmsd12gr4veyg691v&dl=1',
20
+ 'https://www.dropbox.com/scl/fi/r8vgo1xrledlsw7rxq4ar/Tropmed-91-216-g001.jpg?rlkey=6iajn3xoa6zsxtxh4exq4z3p5&dl=1',
21
+ 'https://www.dropbox.com/scl/fi/kxv0q49e92h3fr7ihvqbu/Non_Leprosy_210823_8_jpg.rf.e2d44b96e1bb9b5111b780adec5ba94a.jpg?rlkey=g25iq6vbwqs1glusyv1lgv5a2&dl=1'
22
+ ]
23
+
24
+ def download_file(url, save_name):
25
+ url = url
26
+ if not os.path.exists(save_name):
27
+ file = requests.get(url)
28
+ open(save_name, 'wb').write(file.content)
29
+
30
+ for i, url in enumerate(file_urls):
31
+ if 'mp4' in file_urls[i]:
32
+ download_file(
33
+ file_urls[i],
34
+ f"video.mp4"
35
+ )
36
+ else:
37
+ download_file(
38
+ file_urls[i],
39
+ f"image_{i}.jpg"
40
+ )
41
+
42
+ path = "obj.pt"
43
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path=path, force_reload=True)
44
+ img_path = [['image_0.jpg'], ['image_1.jpg'],['image_2.jpg'],['image_3.jpg'],['image_4.jpg'],['image_5.jpg'],]
45
+ video_path = [['video.mp4']]
46
+
47
+ def show_preds_image_and_labels(image_path):
48
+ image = cv2.imread(image_path)
49
+
50
+ results = model(image_path)
51
+
52
+ pandas_result = results.pandas().xyxy[0]
53
+
54
+ array_results = pandas_result.to_numpy()
55
+
56
+ array_results = array_results.tolist()
57
+
58
+ #print raw results
59
+ print("\nRaw results: ", array_results)
60
+
61
+ array_bounding_box= []
62
+
63
+ array_model_result = []
64
+
65
+ array_model_confidence = []
66
+
67
+ #for labelling bounding box
68
+ for item in array_results:
69
+ array_bounding_box.append([item[0],item[1],item[2],item[3]])
70
+ array_model_result.append(item[6])
71
+ array_model_confidence.append(str(round(item[4],1)*100))
72
+
73
+ for numbers in range(len(array_model_result)):
74
+ x1, y1 = int(math.floor(array_bounding_box[numbers][0])), int(math.floor(array_bounding_box[numbers][1])) # top-left corner
75
+ x2, y2 = int(math.floor(array_bounding_box[numbers][2])), int(math.floor(array_bounding_box[numbers][3])) # bottom-right corner
76
+
77
+ if array_model_result[numbers] == "Lep":
78
+ # draw a rectangle over the image using the bounding box coordinates
79
+
80
+ #if the value of leprosy conf is < 0.45 then label it as NLp to show the max voting value
81
+ if float(array_model_confidence[numbers]) > 45.0:
82
+
83
+ cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0,255), 1)
84
+ cv2.putText(image, array_model_result[numbers] + " " + array_model_confidence[numbers] + "%", (x1, y1-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 1)
85
+
86
+ elif float(array_model_confidence[numbers]) < 45.0:
87
+
88
+ cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255,0), 1)
89
+ cv2.putText(image, "Non Lep" + " " + array_model_confidence[numbers] + "%", (x1, y1-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 1)
90
+
91
+ elif array_model_result[numbers] == "Non Lep":
92
+
93
+ # draw a rectangle over the image using the bounding box coordinates
94
+ cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255,0), 1)
95
+ cv2.putText(image, array_model_result[numbers] + " " + array_model_confidence[numbers] + "%", (x1, y1-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 1)
96
+
97
+ #labelling dictionary
98
+
99
+ array_results_conf_large = []
100
+
101
+ for yolo_results in array_results:
102
+ if yolo_results[4] > 0.45:
103
+ array_results_conf_large.append(yolo_results)
104
+
105
+ print("Large results only: ",array_results_conf_large)
106
+
107
+ num_lep = 0
108
+ confidence_lep = 0
109
+
110
+ num_non_lep = 0
111
+ confidence_non_lep = 0
112
+
113
+ for item in array_results_conf_large:
114
+ if item[6] == "Lep":
115
+ num_lep+=1
116
+ confidence_lep += item[4]
117
+ elif item[6] == "Non Lep":
118
+ num_non_lep+=1
119
+ confidence_non_lep += item[4]
120
+
121
+ labels = {}
122
+
123
+ #if num_lep is more than non lep
124
+ if num_lep > num_non_lep:
125
+ labels["Leprosy"] = round(confidence_lep/num_lep,2)
126
+
127
+ if num_non_lep != 0:
128
+ labels["Non Leprosy"] = round(confidence_non_lep/num_non_lep,2)
129
+ else:
130
+ lower_score_lep_num = 0
131
+ lower_score_lep_conf = 0.0
132
+
133
+ for results_index in range(len(array_model_confidence)):
134
+ if array_model_result[results_index] == "Lep":
135
+ if float(array_model_confidence[results_index]) < 45.0:
136
+ lower_score_lep_num+=1
137
+ lower_score_lep_conf+=float(array_model_confidence[results_index])
138
+
139
+ if lower_score_lep_num != 0:
140
+ labels["Non Leprosy"] = round((lower_score_lep_conf/lower_score_lep_num)/100,2)
141
+ else:
142
+ labels["Non Leprosy"] = 0.0
143
+
144
+ #if num_non_lep is more than lep
145
+ elif num_lep < num_non_lep:
146
+ labels["Non Leprosy"] = round(confidence_non_lep/num_non_lep,2)
147
+
148
+ if num_lep != 0:
149
+ labels["Leprosy"] = round(confidence_lep/num_lep,2)
150
+ else:
151
+ labels["Leprosy"] = 0.0
152
+
153
+ #if num_non_lep and num_lep is equal but they are equal coz they are both 0
154
+ elif num_lep == num_non_lep and num_lep == 0:
155
+ labels["Others"] = 0.9
156
+
157
+ #if num_non_lep and num_lep is equal but they are equal coz they are both 0
158
+ elif num_lep == num_non_lep:
159
+
160
+ #incase of a tie in quantity we compare the mean probability of each
161
+ confidence_lep = round(confidence_lep/num_lep,2)
162
+ confidence_non_lep = round(confidence_non_lep/num_non_lep,2)
163
+
164
+ if confidence_lep > confidence_non_lep:
165
+ labels["Leprosy"] = confidence_lep
166
+ labels["Non Leprosy"] = confidence_non_lep
167
+
168
+ elif confidence_lep < confidence_non_lep:
169
+ labels["Leprosy"] = confidence_lep
170
+ labels["Non Leprosy"] = confidence_non_lep
171
+
172
+ elif confidence_lep == confidence_non_lep:
173
+ labels["Leprosy"] = confidence_lep
174
+ labels["Non Leprosy"] = confidence_non_lep
175
+
176
+ return cv2.cvtColor(image, cv2.COLOR_BGR2RGB),labels
177
+
178
+ inputs_image = [
179
+ gr.components.Image(type="filepath", label="Input Image"),
180
+ ]
181
+ outputs_image = [
182
+ gr.components.Image(type="numpy", label="Output Image"),
183
+ gr.Label(label="Labels with Confidence"),
184
+
185
+ ]
186
+ interface_image = gr.Interface(
187
+ fn=show_preds_image_and_labels,
188
+ inputs=inputs_image,
189
+ outputs=outputs_image,
190
+ title="Leprosy Detection",
191
+ examples=img_path,
192
+ cache_examples=False,
193
+ )
194
+
195
+ gr.TabbedInterface(
196
+ [interface_image],
197
+ tab_names=['Image inference']
198
+ ).queue().launch()
image_0.jpg ADDED
image_1.jpg ADDED
image_2.jpg ADDED
image_3.jpg ADDED
image_4.jpg ADDED

Git LFS Details

  • SHA256: 60117eb09897c88f689eb06dc47fcfc6283a11813b0e64ab2b2dae9dd161098d
  • Pointer size: 131 Bytes
  • Size of remote file: 110 kB
image_5.jpg ADDED
lp1.jpg ADDED
lp2.jpg ADDED
nlp1.jpg ADDED
nlp2.jpg ADDED
obj.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3be9d4f559d07d3bdb661ff619919afcfa2cfbb0a46b92ebf3aaca835b7383f2
3
+ size 14446333
requirements.txt ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # YOLOv5 requirements
2
+ # Usage: pip install -r requirements.txt
3
+
4
+ # Base ------------------------------------------------------------------------
5
+ gitpython>=3.1.30
6
+ matplotlib>=3.3
7
+ numpy>=1.22.2
8
+ opencv-python>=4.1.1
9
+ Pillow>=7.1.2
10
+ psutil # system resources
11
+ PyYAML>=5.3.1
12
+ requests>=2.23.0
13
+ scipy>=1.4.1
14
+ thop>=0.1.1 # FLOPs computation
15
+ torch>=1.8.0 # see https://pytorch.org/get-started/locally (recommended)
16
+ torchvision>=0.9.0
17
+ tqdm>=4.64.0
18
+ ultralytics>=8.0.147
19
+ # protobuf<=3.20.1 # https://github.com/ultralytics/yolov5/issues/8012
20
+
21
+ # Logging ---------------------------------------------------------------------
22
+ # tensorboard>=2.4.1
23
+ # clearml>=1.2.0
24
+ # comet
25
+
26
+ # Plotting --------------------------------------------------------------------
27
+ pandas>=1.1.4
28
+ seaborn>=0.11.0
29
+
30
+ # Export ----------------------------------------------------------------------
31
+ # coremltools>=6.0 # CoreML export
32
+ # onnx>=1.10.0 # ONNX export
33
+ # onnx-simplifier>=0.4.1 # ONNX simplifier
34
+ # nvidia-pyindex # TensorRT export
35
+ # nvidia-tensorrt # TensorRT export
36
+ # scikit-learn<=1.1.2 # CoreML quantization
37
+ # tensorflow>=2.4.0 # TF exports (-cpu, -aarch64, -macos)
38
+ # tensorflowjs>=3.9.0 # TF.js export
39
+ # openvino-dev>=2023.0 # OpenVINO export
40
+
41
+ # Deploy ----------------------------------------------------------------------
42
+ setuptools>=65.5.1 # Snyk vulnerability fix
43
+ # tritonclient[all]~=2.24.0
44
+
45
+ # Extras ----------------------------------------------------------------------
46
+ # ipython # interactive notebook
47
+ # mss # screenshots
48
+ # albumentations>=1.0.3
49
+ # pycocotools>=2.0.6 # COCO mAP
50
+
51
+ gradio