-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
64 lines (51 loc) · 2.09 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# import YOLO model
from ultralytics import YOLO
# import maths functions
import numpy as np
# import image functions
from PIL import Image
# import requests function that grabs an image from a URL
import requests
# import methods to manipulate bytes data in memory
from io import BytesIO
# import custom dataset from Roboflow
from roboflow import Roboflow
# download and unzip Roboflow dataset
rf = Roboflow(api_key="HAijjYs0jW2f55BBoIS0")
project = rf.workspace("machine-learning-yfysx").project("machine-learning-mooc")
dataset = project.version(6).download("yolov8")
# load the standard model or can be the empty .yaml file since we will be overwriting it with custom data
# model = YOLO("yolov8n.pt")
# load the trained model
model = YOLO("runs/detect/train31/weights/best.pt")
# train the model using the Roboflow dataset
# results = model.train(data=dataset.location + "/data.yaml", epochs=150)
# save trained model
# success = model.export(format="onnx")
# prompt user
user_pick = str(input('1 for image in URL or 2 for image in directory path: ')).strip()
if user_pick == "1":
# ask the user for the URL
image_url = str(input('Image URL: ')).strip()
# ask the user for a confidence level
confidence = str(input('Confidence level: ')).strip()
confidence_level = float(confidence)
# send a get request to the URL
response = requests.get(image_url)
# read and save image data
image = Image.open(BytesIO(response.content))
# convert the image into an array
image = np.asarray(image)
# predict the detected objects and output to the user
results = model.predict(image, conf=confidence_level)
elif user_pick == "2":
# ask the user for the file directory
image_path = str(input('Image directory path: ')).strip()
# ask the user for a confidence level
confidence = str(input('Confidence level: ')).strip()
confidence_level = float(confidence)
# predict the detected objects and output to the user
results = model.predict(source='{}'.format(image_path), conf=confidence_level)
else:
print("Invalid choice. Please restart programme.")
quit()