forked from panditakshay402/Hacktoberfest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request panditakshay402#1 from Spyrosigma/Spyrosigma
Added Item-detection using Yolov8
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from PIL import Image | ||
from ultralytics import YOLO | ||
import os | ||
# Load a pretrained YOLOv8n model | ||
model = YOLO('yolov8n.pt') | ||
#THIS IS FOR IMAAAAAAGEEEEEEEEEE DETECTION !! | ||
img_count = 0 | ||
image_paths = [] | ||
image_directory = "F:\\GFG\\Images\\" #enter the path for image folder here ! | ||
for filename in os.listdir(image_directory): | ||
if filename.endswith(".jpg") or filename.endswith(".JPG") or filename.endswith(".jpeg") or filename.endswith(".png"): | ||
image_paths.append(os.path.join(image_directory, filename)) | ||
img_count += 1 | ||
# Now, image_paths contains the paths of the images | ||
print(f'Total number of images in the folder {image_directory} : ',img_count) | ||
# # For example, if you have a directory with multiple image files: | ||
# image path example : 'F:\\GFG\\Images\\img3.jpg' | ||
count = 0 | ||
for i in image_paths: | ||
results = model(i) # results list | ||
count += 1 | ||
# Show the results | ||
for r in results: | ||
im_array = r.plot() # plot a BGR numpy array of predictions | ||
im = Image.fromarray(im_array[..., ::-1]) # RGB PIL image | ||
im.show() # show image | ||
im.save(f'F:\\GFG\\static\\detection_result\\{count}.jpg') # save image |