Projekt-PBR_Sztuczna_Empatia/image_detector/robot_detector.py

60 lines
1.7 KiB
Python
Raw Normal View History

2023-07-05 19:06:09 +02:00
from PIL import Image
import cv2 as cv
import re
def detect_img(yolo, img_path):
detected_robots = []
try:
image = Image.open(img_path)
except:
print('Image open Error! Try again!')
return None
else:
r_image, pred = yolo.detect_image(image)
processed_image = cv.imread(img_path)
# r_image.show()
# r_image.save(img_path)
if not pred:
return None
## FIXME : better list mapping
for prediction in pred:
is_robot_detected = re.search("robot", prediction[0])
if is_robot_detected:
x1 = prediction[1][0]
x2 = prediction[2][0]
y1 = prediction[1][1]
y2 = prediction[2][1]
w = abs(x1 - x2)
h = abs(y1 - y2)
# print(pred)
# print(f'x1: {x1}, x2: {x2}, y1: {y1}, y2: {y2}, w: {w}, h: {h}')
robot_img = processed_image[y1:y1 + h, x1:x1 + w]
robot_img = cv.resize(robot_img, (128, 128), interpolation=cv.INTER_AREA)
robot_pos = w, x1
detected_robots.append((robot_img, robot_pos))
return detected_robots
def get_turn_value(cords):
img_width = 1920
w, x = cords
center_of_object = (x + w) / 2
object_position = center_of_object / img_width
return round(object_position * 100, 2)
def detect_robot(model, img_path):
detected_robots = detect_img(model, img_path)
return detected_robots
# if not detected_robots:
# return None
# robot_position = detected_robots[0][1]
# turn_val = get_turn_value(robot_position)
# return turn_val