Skip to content

Commit c0a01dd

Browse files
authored
Merge pull request #281 from Spyrosigma/main
Added item-detection(from image) using Yolov8
2 parents 777cd17 + ce838d5 commit c0a01dd

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

ML Project/image_detection.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from PIL import Image
2+
from ultralytics import YOLO
3+
import os
4+
# Load a pretrained YOLOv8n model
5+
model = YOLO('yolov8n.pt')
6+
#THIS IS FOR IMAAAAAAGEEEEEEEEEE DETECTION !!
7+
img_count = 0
8+
image_paths = []
9+
image_directory = "F:\\GFG\\Images\\" #enter the path for image folder here !
10+
for filename in os.listdir(image_directory):
11+
if filename.endswith(".jpg") or filename.endswith(".JPG") or filename.endswith(".jpeg") or filename.endswith(".png"):
12+
image_paths.append(os.path.join(image_directory, filename))
13+
img_count += 1
14+
# Now, image_paths contains the paths of the images
15+
print(f'Total number of images in the folder {image_directory} : ',img_count)
16+
# # For example, if you have a directory with multiple image files:
17+
# image path example : 'F:\\GFG\\Images\\img3.jpg'
18+
count = 0
19+
for i in image_paths:
20+
results = model(i) # results list
21+
count += 1
22+
# Show the results
23+
for r in results:
24+
im_array = r.plot() # plot a BGR numpy array of predictions
25+
im = Image.fromarray(im_array[..., ::-1]) # RGB PIL image
26+
im.show() # show image
27+
im.save(f'F:\\GFG\\static\\detection_result\\{count}.jpg') # save image

0 commit comments

Comments
 (0)