Upload 14 files
Browse files- Modelos/best.pt +3 -0
- TrashDetect.py +60 -0
- main.py +228 -0
- setUp/Canva.png +0 -0
- setUp/carton.png +0 -0
- setUp/cartontxt.png +0 -0
- setUp/medical.png +0 -0
- setUp/medicaltxt.png +0 -0
- setUp/metal.png +0 -0
- setUp/metaltxt.png +0 -0
- setUp/plastico.png +0 -0
- setUp/plasticotxt.png +0 -0
- setUp/vidrio.png +0 -0
- setUp/vidriotxt.png +0 -0
Modelos/best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c520d4cf2558ae76288879a4d9bebac5c592b6affa7b0f06ef54de967b431cc6
|
| 3 |
+
size 87625726
|
TrashDetect.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Importamos librerias
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import cv2
|
| 4 |
+
import math
|
| 5 |
+
|
| 6 |
+
# Modelo
|
| 7 |
+
model = YOLO('Modelos/best.pt')
|
| 8 |
+
|
| 9 |
+
# Cap
|
| 10 |
+
cap = cv2.VideoCapture(0)
|
| 11 |
+
cap.set(3, 1280)
|
| 12 |
+
cap.set(4, 720)
|
| 13 |
+
|
| 14 |
+
# Clases
|
| 15 |
+
clsName = ['Metal', 'Glass', 'Plastic', 'Carton', 'Medical']
|
| 16 |
+
|
| 17 |
+
# Inference
|
| 18 |
+
while True:
|
| 19 |
+
# Frames
|
| 20 |
+
ret, frame = cap.read()
|
| 21 |
+
|
| 22 |
+
# Yolo | AntiSpoof
|
| 23 |
+
results = model(frame, stream=True, verbose=False)
|
| 24 |
+
for res in results:
|
| 25 |
+
# Box
|
| 26 |
+
boxes = res.boxes
|
| 27 |
+
for box in boxes:
|
| 28 |
+
# Bounding box
|
| 29 |
+
x1, y1, x2, y2 = box.xyxy[0]
|
| 30 |
+
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
| 31 |
+
|
| 32 |
+
# Error < 0
|
| 33 |
+
if x1 < 0: x1 = 0
|
| 34 |
+
if y1 < 0: y1 = 0
|
| 35 |
+
if x2 < 0: x2 = 0
|
| 36 |
+
if y2 < 0: y2 = 0
|
| 37 |
+
|
| 38 |
+
# Class
|
| 39 |
+
cls = int(box.cls[0])
|
| 40 |
+
|
| 41 |
+
# Confidence
|
| 42 |
+
conf = math.ceil(box.conf[0])
|
| 43 |
+
print(f"Clase: {cls} Confidence: {conf}")
|
| 44 |
+
|
| 45 |
+
if conf > 0:
|
| 46 |
+
# Draw
|
| 47 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
| 48 |
+
cv2.putText(frame, f'{clsName[cls]} {int(conf * 100)}%', (x1, y1 - 20),
|
| 49 |
+
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2)
|
| 50 |
+
|
| 51 |
+
# Show
|
| 52 |
+
cv2.imshow("Waste Detect", frame)
|
| 53 |
+
|
| 54 |
+
# Close
|
| 55 |
+
t = cv2.waitKey(5)
|
| 56 |
+
if t == 27:
|
| 57 |
+
break
|
| 58 |
+
|
| 59 |
+
cap.release()
|
| 60 |
+
cv2.destroyAllWindows()
|
main.py
ADDED
|
@@ -0,0 +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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Libraries
|
| 2 |
+
from tkinter import *
|
| 3 |
+
from PIL import Image, ImageTk
|
| 4 |
+
import imutils
|
| 5 |
+
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
+
from ultralytics import YOLO
|
| 8 |
+
import math
|
| 9 |
+
|
| 10 |
+
def clean_lbl():
|
| 11 |
+
# Clean
|
| 12 |
+
lblimg.config(image='')
|
| 13 |
+
lblimgtxt.config(image='')
|
| 14 |
+
|
| 15 |
+
def images(img, imgtxt):
|
| 16 |
+
img = img
|
| 17 |
+
imgtxt = imgtxt
|
| 18 |
+
|
| 19 |
+
# Img Detect
|
| 20 |
+
img = np.array(img, dtype="uint8")
|
| 21 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 22 |
+
img = Image.fromarray(img)
|
| 23 |
+
|
| 24 |
+
img_ = ImageTk.PhotoImage(image=img)
|
| 25 |
+
lblimg.configure(image=img_)
|
| 26 |
+
lblimg.image = img_
|
| 27 |
+
|
| 28 |
+
# Img Text
|
| 29 |
+
imgtxt = np.array(imgtxt, dtype="uint8")
|
| 30 |
+
imgtxt = cv2.cvtColor(imgtxt, cv2.COLOR_BGR2RGB)
|
| 31 |
+
imgtxt = Image.fromarray(imgtxt)
|
| 32 |
+
|
| 33 |
+
img_txt = ImageTk.PhotoImage(image=imgtxt)
|
| 34 |
+
lblimgtxt.configure(image=img_txt)
|
| 35 |
+
lblimgtxt.image = img_txt
|
| 36 |
+
|
| 37 |
+
# Scanning Function
|
| 38 |
+
def Scanning():
|
| 39 |
+
global img_metal, img_glass, img_plastic, img_carton, img_medical
|
| 40 |
+
global img_metaltxt, img_glasstxt, img_plastictxt, img_cartontxt, img_medicaltxt, pantalla
|
| 41 |
+
global lblimg, lblimgtxt
|
| 42 |
+
|
| 43 |
+
# Interfaz
|
| 44 |
+
lblimg = Label(pantalla)
|
| 45 |
+
lblimg.place(x=75, y=260)
|
| 46 |
+
|
| 47 |
+
lblimgtxt = Label(pantalla)
|
| 48 |
+
lblimgtxt.place(x=995, y=310)
|
| 49 |
+
detect = False
|
| 50 |
+
|
| 51 |
+
# Read VideoCapture
|
| 52 |
+
if cap is not None:
|
| 53 |
+
ret, frame = cap.read()
|
| 54 |
+
frame_show =cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 55 |
+
|
| 56 |
+
# True
|
| 57 |
+
if ret == True:
|
| 58 |
+
# Yolo | AntiSpoof
|
| 59 |
+
results = model(frame, stream=True, verbose=False)
|
| 60 |
+
for res in results:
|
| 61 |
+
# Box
|
| 62 |
+
boxes = res.boxes
|
| 63 |
+
for box in boxes:
|
| 64 |
+
detect = True
|
| 65 |
+
# Bounding box
|
| 66 |
+
x1, y1, x2, y2 = box.xyxy[0]
|
| 67 |
+
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
| 68 |
+
|
| 69 |
+
# Error < 0
|
| 70 |
+
if x1 < 0: x1 = 0
|
| 71 |
+
if y1 < 0: y1 = 0
|
| 72 |
+
if x2 < 0: x2 = 0
|
| 73 |
+
if y2 < 0: y2 = 0
|
| 74 |
+
|
| 75 |
+
# Class
|
| 76 |
+
cls = int(box.cls[0])
|
| 77 |
+
|
| 78 |
+
# Confidence
|
| 79 |
+
conf = math.ceil(box.conf[0])
|
| 80 |
+
#print(f"Clase: {cls} Confidence: {conf}")
|
| 81 |
+
# Metal
|
| 82 |
+
if cls == 0:
|
| 83 |
+
# Draw
|
| 84 |
+
cv2.rectangle(frame_show, (x1, y1), (x2, y2), (255, 255, 0), 2)
|
| 85 |
+
# Text
|
| 86 |
+
text = f'{clsName[cls]} {int(conf) * 100}%'
|
| 87 |
+
sizetext = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
|
| 88 |
+
dim = sizetext[0]
|
| 89 |
+
baseline = sizetext[1]
|
| 90 |
+
# Rect
|
| 91 |
+
cv2.rectangle(frame_show, (x1, y1 - dim[1] - baseline), (x1 + dim[0], y1 + baseline), (0, 0, 0),cv2.FILLED)
|
| 92 |
+
cv2.putText(frame_show, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2)
|
| 93 |
+
|
| 94 |
+
# Clasificacion
|
| 95 |
+
images(img_metal, img_metaltxt)
|
| 96 |
+
|
| 97 |
+
if cls == 1:
|
| 98 |
+
# Draw
|
| 99 |
+
cv2.rectangle(frame_show, (x1, y1), (x2, y2), (255, 255, 255), 2)
|
| 100 |
+
# Text
|
| 101 |
+
text = f'{clsName[cls]} {int(conf) * 100}%'
|
| 102 |
+
sizetext = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
|
| 103 |
+
dim = sizetext[0]
|
| 104 |
+
baseline = sizetext[1]
|
| 105 |
+
# Rect
|
| 106 |
+
cv2.rectangle(frame_show, (x1, y1 - dim[1] - baseline), (x1 + dim[0], y1 + baseline),
|
| 107 |
+
(0, 0, 0), cv2.FILLED)
|
| 108 |
+
cv2.putText(frame_show, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
|
| 109 |
+
|
| 110 |
+
# Clasificacion
|
| 111 |
+
images(img_glass, img_glasstxt)
|
| 112 |
+
|
| 113 |
+
if cls == 2:
|
| 114 |
+
# Draw
|
| 115 |
+
cv2.rectangle(frame_show, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
| 116 |
+
# Text
|
| 117 |
+
text = f'{clsName[cls]} {int(conf) * 100}%'
|
| 118 |
+
sizetext = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
|
| 119 |
+
dim = sizetext[0]
|
| 120 |
+
baseline = sizetext[1]
|
| 121 |
+
# Rect
|
| 122 |
+
cv2.rectangle(frame_show, (x1, y1 - dim[1] - baseline), (x1 + dim[0], y1 + baseline),
|
| 123 |
+
(0, 0, 0), cv2.FILLED)
|
| 124 |
+
cv2.putText(frame_show, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
|
| 125 |
+
|
| 126 |
+
# Clasificacion
|
| 127 |
+
images(img_plastic, img_plastictxt)
|
| 128 |
+
|
| 129 |
+
if cls == 3:
|
| 130 |
+
# Draw
|
| 131 |
+
cv2.rectangle(frame_show, (x1, y1), (x2, y2), (150, 150, 150), 2)
|
| 132 |
+
# Text
|
| 133 |
+
text = f'{clsName[cls]} {int(conf) * 100}%'
|
| 134 |
+
sizetext = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
|
| 135 |
+
dim = sizetext[0]
|
| 136 |
+
baseline = sizetext[1]
|
| 137 |
+
# Rect
|
| 138 |
+
cv2.rectangle(frame_show, (x1, y1 - dim[1] - baseline), (x1 + dim[0], y1 + baseline),
|
| 139 |
+
(0, 0, 0), cv2.FILLED)
|
| 140 |
+
cv2.putText(frame_show, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (150, 150, 150), 2)
|
| 141 |
+
|
| 142 |
+
# Clasificacion
|
| 143 |
+
images(img_carton, img_cartontxt)
|
| 144 |
+
|
| 145 |
+
if cls == 4:
|
| 146 |
+
# Draw
|
| 147 |
+
cv2.rectangle(frame_show, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
| 148 |
+
# Text
|
| 149 |
+
text = f'{clsName[cls]} {int(conf) * 100}%'
|
| 150 |
+
sizetext = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
|
| 151 |
+
dim = sizetext[0]
|
| 152 |
+
baseline = sizetext[1]
|
| 153 |
+
# Rect
|
| 154 |
+
cv2.rectangle(frame_show, (x1, y1 - dim[1] - baseline), (x1 + dim[0], y1 + baseline),
|
| 155 |
+
(0, 0, 0), cv2.FILLED)
|
| 156 |
+
cv2.putText(frame_show, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
|
| 157 |
+
|
| 158 |
+
# Clasificacion
|
| 159 |
+
images(img_medical, img_medicaltxt)
|
| 160 |
+
|
| 161 |
+
if detect == False:
|
| 162 |
+
# Clean
|
| 163 |
+
clean_lbl()
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
# Resize
|
| 167 |
+
frame_show = imutils.resize(frame_show, width=640)
|
| 168 |
+
|
| 169 |
+
# Convertimos el video
|
| 170 |
+
im = Image.fromarray(frame_show)
|
| 171 |
+
img = ImageTk.PhotoImage(image=im)
|
| 172 |
+
|
| 173 |
+
# Mostramos en el GUI
|
| 174 |
+
lblVideo.configure(image=img)
|
| 175 |
+
lblVideo.image = img
|
| 176 |
+
lblVideo.after(10, Scanning)
|
| 177 |
+
|
| 178 |
+
else:
|
| 179 |
+
cap.release()
|
| 180 |
+
|
| 181 |
+
# main
|
| 182 |
+
def ventana_principal():
|
| 183 |
+
global cap, lblVideo, model, clsName, img_metal, img_glass, img_plastic, img_carton, img_medical
|
| 184 |
+
global img_metaltxt, img_glasstxt, img_plastictxt, img_cartontxt, img_medicaltxt, pantalla
|
| 185 |
+
# Ventana principal
|
| 186 |
+
pantalla = Tk()
|
| 187 |
+
pantalla.title("RECICLAJE INTELIGENTE")
|
| 188 |
+
pantalla.geometry("1280x720")
|
| 189 |
+
|
| 190 |
+
# Background
|
| 191 |
+
imagenF = PhotoImage(file="setUp/Canva.png")
|
| 192 |
+
background = Label(image=imagenF, text="Inicio")
|
| 193 |
+
background.place(x=0, y=0, relwidth=1, relheight=1)
|
| 194 |
+
|
| 195 |
+
# Clases: 0 -> Metal | 1 -> Glass | 2 -> Plastic | 3 -> Carton | 4 -> Medical
|
| 196 |
+
# Model
|
| 197 |
+
model = YOLO('Modelos/best.pt')
|
| 198 |
+
|
| 199 |
+
# Clases
|
| 200 |
+
clsName = ['Metal', 'Glass', 'Plastic', 'Carton', 'Medical']
|
| 201 |
+
|
| 202 |
+
# Images
|
| 203 |
+
img_metal = cv2.imread("setUp/metal.png")
|
| 204 |
+
img_glass = cv2.imread("setUp/vidrio.png")
|
| 205 |
+
img_plastic = cv2.imread("setUp/plastico.png")
|
| 206 |
+
img_carton = cv2.imread("setUp/carton.png")
|
| 207 |
+
img_medical = cv2.imread("setUp/medical.png")
|
| 208 |
+
img_metaltxt = cv2.imread("setUp/metaltxt.png")
|
| 209 |
+
img_glasstxt = cv2.imread("setUp/vidriotxt.png")
|
| 210 |
+
img_plastictxt = cv2.imread("setUp/plasticotxt.png")
|
| 211 |
+
img_cartontxt = cv2.imread("setUp/cartontxt.png")
|
| 212 |
+
img_medicaltxt = cv2.imread("setUp/medicaltxt.png")
|
| 213 |
+
|
| 214 |
+
# Video
|
| 215 |
+
lblVideo = Label(pantalla)
|
| 216 |
+
lblVideo.place(x=320, y=180)
|
| 217 |
+
|
| 218 |
+
# Elegimos la camara
|
| 219 |
+
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
|
| 220 |
+
cap.set(3, 1280)
|
| 221 |
+
cap.set(4, 720)
|
| 222 |
+
Scanning()
|
| 223 |
+
|
| 224 |
+
# Eject
|
| 225 |
+
pantalla.mainloop()
|
| 226 |
+
|
| 227 |
+
if __name__ == "__main__":
|
| 228 |
+
ventana_principal()
|
setUp/Canva.png
ADDED
|
setUp/carton.png
ADDED
|
setUp/cartontxt.png
ADDED
|
setUp/medical.png
ADDED
|
setUp/medicaltxt.png
ADDED
|
setUp/metal.png
ADDED
|
setUp/metaltxt.png
ADDED
|
setUp/plastico.png
ADDED
|
setUp/plasticotxt.png
ADDED
|
setUp/vidrio.png
ADDED
|
setUp/vidriotxt.png
ADDED
|