[CODE lang="python" title="Python"]from vehicle_controller import VehicleController
import numpy as np
import cv2
from PIL import ImageGrab
import time
controller = VehicleController("127.0.0.1")
color_find = 0
tolerans_x = 10
tolerans_y = 10
while True:
im = np.array(ImageGrab.grab(bbox=(0, 0, 800, 600)))
kernel = np.ones((7, 7), np.uint8)
screenContour = im.copy()
screenBlur = cv2.GaussianBlur(im, (3, 3), 4)
screenCanny = cv2.Canny(screenBlur, 180, 180)
screenDil = cv2.dilate(screenCanny, kernel, iterations=0)
contours, hierarchy = cv2.findContours(screenDil, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
color_find = 0
for cnt in contours:
area = cv2.contourArea(cnt)
if area > 1000:
peri = cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, 0.002 * peri, True)
if len(approx) >= 4:
x, y, w, h = cv2.boundingRect(approx)
noktax = x + (w // 2)
noktay = y + (h // 2)
cv2.rectangle(screenContour, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(screenContour, ".", (noktax, noktay), cv2.FONT_ITALIC, 1, (255, 0, 0), 2)
color_find = 1
break
cv2.imshow("Screen", cv2.cvtColor(screenContour, cv2.COLOR_BGR2RGB))
if color_find == 0:
controller.SetSpeedDirection(5000)
else:
controller.SetSpeedDirection(0)
if (abs(noktax - 400) < tolerans_x) and (abs(noktay - 300) < tolerans_y):
controller.SetSpeedX(0)
controller.SetSpeedY(0)
else:
if (noktax > 400 + tolerans_x):
controller.SetSpeedX(-100)
elif (noktax < 400 - tolerans_x):
controller.SetSpeedX(100)
if (noktay > 300 + tolerans_y):
controller.SetSpeedY(-100)
elif (noktay < 300 - tolerans_y):
controller.SetSpeedY(100)
controller.Send()
if cv2.waitKey(1) & 0xFF == ord("q"):
break
[/CODE]
Deneyip sonuçları yazın.