iamsuman commited on
Commit
75f3127
·
1 Parent(s): fa43905

added medical waste detector

Browse files
Files changed (6) hide show
  1. .gitignore +11 -0
  2. README.md +70 -14
  3. app.py +59 -0
  4. labels.json +27 -0
  5. model/best.pt +3 -0
  6. requirements.txt +6 -0
.gitignore ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ flagged/
2
+ *.png
3
+ *.jpg
4
+ *.mp4
5
+ *.jpeg
6
+ *.mkv
7
+ .DS_Store
8
+ gradio_cached_examples/
9
+ venv/
10
+ __pycache__
11
+ flagged
README.md CHANGED
@@ -1,14 +1,70 @@
1
- ---
2
- title: Medical Waste Detector
3
- emoji: 🚀
4
- colorFrom: gray
5
- colorTo: yellow
6
- sdk: gradio
7
- sdk_version: 5.25.2
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- short_description: Detection of Medical Waste
12
- ---
13
-
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 🩺 Medical Waste Classifier
2
+
3
+ This project uses a YOLOv8 model with a Gradio interface to detect and classify medical waste types based on an uploaded image.
4
+
5
+ ## 💡 Features
6
+
7
+ - Upload medical waste images
8
+ - Object detection using YOLOv8
9
+ - Classification by:
10
+ - Waste name
11
+ - Waste type (Infectious, Pathological, etc.)
12
+ - Color code (Red, Blue, Green)
13
+
14
+ ## 🧠 Model
15
+
16
+ Make sure your YOLOv8 model is trained on the relevant medical waste classes and saved as `best.pt`.
17
+
18
+ ## 📁 Folder Structure
19
+
20
+ ```
21
+ medical-waste-detector/
22
+ ├── model/
23
+ │ └── best.pt
24
+ ├── .env
25
+ ├── app.py
26
+ ├── labels.json
27
+ ├── requirements.txt
28
+ └── README.md
29
+ ```
30
+
31
+ ## ⚙️ Setup Instructions
32
+
33
+ 1. **Clone the project**
34
+ ```bash
35
+ git clone https://github.com/sumn2u/medical-waste-detector
36
+ cd medical-waste-detector
37
+ ```
38
+
39
+ 2. **Install dependencies**
40
+ ```bash
41
+ pip install -r requirements.txt
42
+ ```
43
+
44
+ 3. **Run the app**
45
+ ```bash
46
+ python app.py
47
+ ```
48
+
49
+ ## 🧾 Example Labels
50
+
51
+ ```json
52
+ {
53
+ "Glove Pair Latex": ["Infectious Waste", "Red"],
54
+ "Glass Equipment Packaging": ["Non Biodegradable", "Blue"]
55
+ }
56
+ ```
57
+
58
+ ## 🧼 Waste Color Codes
59
+
60
+ | Type | Color Code |
61
+ |----------------------------------|------------|
62
+ | Pathological Waste | Red |
63
+ | Infectious Waste | Red |
64
+ | General/Biodegradable Waste | Green |
65
+ | General/Non-Biodegradable Waste | Blue |
66
+ | Infectious Plastic Waste | Red |
67
+
68
+ ## 🔒 License
69
+
70
+ MIT License
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ from PIL import Image
4
+ import json
5
+
6
+ # Load classification model and label metadata
7
+ model = YOLO("model/best.pt")
8
+ with open("labels.json") as f:
9
+ labels_info = json.load(f)
10
+
11
+ # Helper: Try to best match the class name
12
+ def get_label_info(name):
13
+ name = name.replace("_", " ")
14
+ if name in labels_info:
15
+ return labels_info[name]
16
+ title_name = name.title()
17
+ if title_name in labels_info:
18
+ return labels_info[title_name]
19
+ capitalized_name = " ".join(word.capitalize() for word in name.split())
20
+ if capitalized_name in labels_info:
21
+ return labels_info[capitalized_name]
22
+ print(f"Label '{name}' not found. Available labels are: {list(labels_info.keys())}")
23
+ return ["Unknown", "Unknown"]
24
+
25
+ # Classification function
26
+ def classify_image(img, conf_threshold= 0.25, iou_threshold=0.45):
27
+ results = model.predict(source=img, conf=conf_threshold, iou=iou_threshold)
28
+ if not results:
29
+ return None, [["No prediction", "-", "-"]]
30
+ result = results[0]
31
+ annotated_img = result.plot()
32
+ if result.probs is not None:
33
+ probs = result.probs.data.tolist()
34
+ max_index = probs.index(max(probs))
35
+ raw_name = model.names[max_index]
36
+ waste_type, color_code = get_label_info(raw_name)
37
+ waste_details = [[raw_name, waste_type, color_code]]
38
+ else:
39
+ waste_details = [["No classification", "-", "-"]]
40
+ return Image.fromarray(annotated_img), waste_details
41
+
42
+ # Gradio interface
43
+ iface = gr.Interface(
44
+ fn=classify_image,
45
+ inputs=[
46
+ gr.Image(type="pil", label="Upload Image"),
47
+ # gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence Threshold"),
48
+ # gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU Threshold"),
49
+ ],
50
+ outputs=[
51
+ gr.Image(label="Result"),
52
+ gr.Dataframe(headers=["Name", "Type", "Color Code"], label="Details")
53
+ ],
54
+ title="Medical Waste Detection",
55
+ description="DWaste uses advanced AI to classify and sort medical waste into various categories to streamline waste management processes for healthcare facilities. Upload an image to classify the medical waste type.<p><strong>Disclaimer:</strong> This tool is for informational purposes only. Predictions made by the AI model may not always be accurate. Please use the results cautiously and verify if necessary.</p>",
56
+ article="<h3>Waste Classification Table</h3><p>Here is the mapping of waste items with their associated types and color codes:</p><table border='1'><tr><th>Item</th><th>Type</th><th>Color Code</th></tr><tr><td>Mask</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Gauze</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Glass Equipment Packaging</td><td>Non Biodegradable</td><td>Blue</td></tr><tr><td>Glove Pair Latex</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Glove Pair Nitrile</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Glove Pair Surgery</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Glove Single Latex</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Glove Single Nitrile</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Glove Single Surgery</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Gloves</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Mask</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Medical Cap</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Medical Glasses</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Metal Equipment Packaging</td><td>General/ Non-Biodegradable Waste</td><td>Blue</td></tr><tr><td>Organic Waste</td><td>General/ Biodegradable Waste</td><td>Green</td></tr><tr><td>Paper Equipment Packaging</td><td>General/ Non-Biodegradable Waste</td><td>Blue</td></tr><tr><td>Plastic Equipment Packaging</td><td>General/ Non-Biodegradable Waste</td><td>Blue</td></tr><tr><td>Shoe Cover Pair</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Shoe Cover Single</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Syringe</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Syringe Needle</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Test Tube</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Tweezers</td><td>Infectious Waste</td><td>Red</td></tr><tr><td>Urine Bag</td><td>Infectious Plastic Waste</td><td>Red</td></tr></table><br><h3>General Waste Categories</h3><p>Here is the mapping of waste groups, types, color codes, and categories:</p><table border='1'><tr><th>Group</th><th>Type</th><th>Color Code</th><th>Category</th></tr><tr><td>General</td><td>Bio-Degradable</td><td>Green</td><td>Organic wastes; peels of vegetables and fruits; rotten or stale foods</td></tr><tr><td>General</td><td>Non-Biodegradable</td><td>Blue</td><td>Paper(A4, Dublex- Medicine Covers, Normal Papers), Plastic(Polythene Bags, Wrappers, Packaging Plastic Materials, Cling Wraps), Bottles (Water, Soft Drinks), vials, saline Bottles</td></tr><tr><td>Infectious</td><td>Infectious</td><td>Red</td><td>Cotton, Gauze, Used Gloves, IV sets, blood bags, urine bag, used PPE, diapers, pad, sputum containers, collection tubes, Dialyzer</td></tr><tr><td>Infectious</td><td>Pathological</td><td>Red</td><td>Body organs or tissues, placenta</td></tr><tr><td>Infectious</td><td>Cytotoxic</td><td>Red</td><td>Unused or Expired cytotoxic drugs, chemotherapy agents</td></tr><tr><td>Infectious</td><td>Pharmaceutical</td><td>Red</td><td>Expired medicines</td></tr><tr><td>Infectious</td><td>Sharps</td><td>Red</td><td>Needles, Blades, Scalpels, Broken glass, Lancets, Suture needles</td></tr><tr><td>Infectious</td><td>Chemical</td><td>Yellow</td><td>Discarded chemicals, reagents, solvents, disinfection</td></tr><tr><td>Infectious</td><td>Radioactive</td><td>Black</td><td>X-Ray films, radio isotopes</td></tr></table><br><p>For more information about Dwaste, visit our website: <a href='http://dwaste.live' target='_blank'>dwaste.live</a></p>"
57
+ )
58
+
59
+ iface.launch()
labels.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "Body Tissue or Organ": ["Pathological Waste", "Red"],
3
+ "Gauze": ["Infectious Waste", "Red"],
4
+ "Glass Equipment Packaging": ["Non Biodegradable", "Blue"],
5
+ "Glove Pair Latex": ["Infectious Waste", "Red"],
6
+ "Glove Pair Nitrile": ["Infectious Waste", "Red"],
7
+ "Glove Pair Surgery": ["Infectious Waste", "Red"],
8
+ "Glove Single Latex": ["Infectious Waste", "Red"],
9
+ "Glove Single Nitrile": ["Infectious Waste", "Red"],
10
+ "Glove Single Surgery": ["Infectious Waste", "Red"],
11
+ "Gloves": ["Infectious Waste", "Red"],
12
+ "Mask": ["Infectious Waste", "Red"],
13
+ "Medical Cap": ["Infectious Waste", "Red"],
14
+ "Medical Glasses": ["Infectious Waste", "Red"],
15
+ "Metal Equipment Packaging": ["General/ Non-Biodegradable Waste", "Blue"],
16
+ "Organic Waste": ["General/ Biodegradable Waste", "Green"],
17
+ "Paper Equipment Packaging": ["General/ Non-Biodegradable Waste", "Blue"],
18
+ "Plastic Equipment Packaging": ["General/ Non-Biodegradable Waste", "Blue"],
19
+ "Shoe Cover Pair": ["Infectious Waste", "Red"],
20
+ "Shoe Cover Single": ["Infectious Waste", "Red"],
21
+ "Syringe": ["Infectious Waste", "Red"],
22
+ "Syringe Needle": ["Infectious Waste", "Red"],
23
+ "Test Tube": ["Infectious Waste", "Red"],
24
+ "Tweezers": ["Infectious Waste", "Red"],
25
+ "Urine Bag": ["Infectious Plastic Waste", "Red"]
26
+ }
27
+
model/best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3e33ef8197d3df71798b42e5990687117b84311f600d0ad46acd93a0cff7e929
3
+ size 3020801
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio==4.10.0
2
+ ultralytics
3
+ Pillow
4
+ pydantic==2.8.2
5
+ pydantic-core==2.20.1
6
+ fastapi==0.112.4