Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

1484

Çalışkan
Katılım
26 Aralık 2023
Mesajlar
21
Beğeniler
17
Arkadaşlar merhaba, ben bir otonom kodu yazıyorum fakat bir yerde bana yardım gerek. Aşağıdaki kod yazdığım normal koddan küçük bir parça. Burada color_find değişkeni normalde 0 ve 0 olunca araç etrafında dönmeye başlıyor. Arama yapmak için ben bunu kodda ne zaman 1 olması gerektiğini atadım. Color_find 1 olduğunda aracı bulduğu şeye ortalaması içinde bir kod yazdım fakat araç ortaladıktan sonra yine de sağ, sol, yukarı, aşağı çok az bir biçimde oynamaya devam ediyor. Bunun çözümünü nasıl yapabilirim?
Örnek video:

if color_find == 0:
controller.SetSpeedDirection(5000)
controller.Send()

if color_find == 1:
controller.SetSpeedDirection(0)
controller.Send()

if (noktax > 400):
controller.SetSpeedX(-100)

if (noktax < 400):
controller.SetSpeedX(100)

if (noktay > 300):
controller.SetSpeedY(-100)

if (noktay < 300):
controller.SetSpeedY(100)
 
Çözüm
[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.
Hocam benim istedğim döngü kırmak değil ki zaten döngüyü kırsam renk algılama dönüsü de kırılı benim istedğim motorları durdurmak için.
 
[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

while True:

print(color_find)

im = np.array(ImageGrab.grab(bbox=(0, 0, 800, 600)))

kernel = np.ones((7, 7))

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)

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)

nokta = (x + (w // 2)), (y + (h // 2))

noktax = x + (w // 2)

noktay = y + (h // 2)

cv2.rectangle(screenContour, (x, y), (x + w, y + h), (0, 255, 0), 2)

cv2.putText(screenContour, ".", (nokta), cv2.FONT_ITALIC, 1, (255, 0, 0), 2)

alan = w * h

color_find = 1

cv2.imshow("Screen", cv2.cvtColor(screenContour, cv2.COLOR_BGR2RGB))

if color_find == 0:

controller.SetSpeedDirection(5000)

controller.Send()

if color_find == 1:

controller.SetSpeedDirection(0)

controller.Send()

if (noktax > 400):

controller.SetSpeedX(-100)

if (noktax < 400):

controller.SetSpeedX(100)


if (noktay > 300):

controller.SetSpeedY(-100)

if (noktay < 300):

controller.SetSpeedY(100)

if cv2.waitKey(1) & 0xFF == ord("q"):

break[/CODE]

Hocam PID yapıcaktım fakat bir sıkıntı çıktı yapamadım.

Hocam bu arada bu controller. olayları smilasyonda test etmek için.
 
Son düzenleyen: Moderatör:
[CODE lang="python" title="Düzenlenmiş Kod"]
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 # x koordinatı için
tolerans_y = 10 # y koordinatı için

while True:
print(color_find)
im = np.array(ImageGrab.grab(bbox=(0, 0, 800, 600)))
kernel = np.ones((7, 7))
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)

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)
nokta = (x + (w // 2)), (y + (h // 2))
noktax = x + (w // 2)
noktay = y + (h // 2)
cv2.rectangle(screenContour, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(screenContour, ".", (nokta), cv2.FONT_ITALIC, 1, (255, 0, 0), 2)
alan = w * h
color_find = 1

cv2.imshow("Screen", cv2.cvtColor(screenContour, cv2.COLOR_BGR2RGB))

if color_find == 0:
controller.SetSpeedDirection(5000)
controller.Send()
else:

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ı yazar mısınız? Ekleme yaptığım kodu da ayrı olarak paylaşıyorum.

[CODE lang="python" title="Pyhon Eklenen Kod"]
tolerans_x = 10
tolerans_y = 10

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)
[/CODE]
 
Hocam çok teşekkürler 15 dakika içinde eve geçince sonuçları size atacağım teşekkür ederim.

Hocam kodu test ettim eğer kapı önünde başlıyorsa sorunsuz çalışıyor fakat eğer kapı önünde değilse ve arama yapacaksa sıkıntı çıkıyor video burada:

Yani dönmeyi durdurmadığı için oluyor sanırsam.

[CODE lang="python" title="Değişen kod"] if color_find ==1:
controller.SetSpeedDirection(0)
controller.Send()

if color_find == 0:
controller.SetSpeedDirection(5000)
controller.Send()

else:

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]

Hocam koda

if color_find ==1:
controller.SetSpeedDirection(0)
controller.Send()

ekleyerek sorunu çözdüm yardımlarınız için çok teşekkür ederim.
 
Son düzenleme:
[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.
 
Çözüm