Raspberry Pi 4 Python kodu yavaş çalışıyor

466609

Hectopat
Katılım
5 Haziran 2021
Mesajlar
2.691
Makaleler
3
Çözümler
26
Merhaba, yazdığım kodu bilgisayarda sorunsuz çalıştırabildim fakat donanımlarının yetersizliği sebebiyle Raspberry Pi 4 üzerinde çalıştığımda çok yavaş(2-3 saniyelik gecikmeyle) çalışıyor. Bunun için kodu nasıl düzenlemeliyim?

Python:
import cv2 as cv2
import numpy as np
#import RPi.GPIO as GPIO
from colorama import Fore, Back, Style
#-----------------------------------------------------------------------------------------------------------------------------------------------
flow_state = False

marker_radius = 5
right_line_color = (0,0,255)
left_line_color = (255,0,0)

primary_perspective = np.float32([[(400,500),(1000,500),(230,715),(1200,715)]])

lower_white, upper_white = (np.array([50, 50, 50], dtype = np.uint8),
                            np.array([255, 255, 255], dtype = np.uint8))
lower_black, upper_black = (np.array([50, 50, 50], dtype = np.uint8),
                            np.array([50, 50, 50], dtype = np.uint8))

#GPIO_out = {}
#GPIO.setmode(GPIO.BCM)

try:
    capture = cv2.VideoCapture(0)
except:
    flow_state = False
while flow_state:
    ret, frame = capture.read()
    if ret == True:
        frame720 = cv2.resize(frame, (1280,720))
        height, width = frame720.shape[:2]
        src = np.float32([[(350,500),(1000,500),(230,715),(1200,715)]])
        dst = np.float32([[(0,0),(1280,0),(0,720),(1280,720)]])
        trs = cv2.getPerspectiveTransform(src, dst)
        warped = cv2.warpPerspective(frame, trs, (width, height))
        warped = cv2.resize(warped, (640,480))

        rgb = cv2.cvtColor(warped, cv2.COLOR_BGR2RGB)

        white_mask = cv2.inRange(rgb, lower_white, upper_white)
        black_mask = cv2.inRange(rgb, lower_black, upper_black)
        combined_mask = cv2.bitwise_or(white_mask, black_mask)
     
        canny = cv2.Canny(combined_mask, 50, 150)

        zeros_frame = np.zeros((480, 640), dtype= np.uint8)
        zeros_frame[:, :320] = combined_mask[:, :320]
        zeros_frame[:, 320:] = 0
        left_lines = cv2.HoughLinesP(zeros_frame, 2, np.pi/180, 100, np.array([]), minLineLength=40, maxLineGap=5)
     
        zeros_frame[:, 320:] = combined_mask[:, 320:]
        zeros_frame[:, :320] = 0
        right_lines = cv2.HoughLinesP(zeros_frame, 2, np.pi/180, 100, np.array([]), minLineLength=40, maxLineGap=5)
        if left_lines is not None and right_lines is not None:
            for line_left in left_lines:
                x1, y1, x2, y2 = line_left.reshape(4)
                left_line_x = x1
                left_line_y = x2
            for line_right in right_lines:
                x1, y1, x2, y2 = line_right.reshape(4)
                right_line_x = x1
                right_line_y = x2
            print(left_line_x, right_line_x)
        cv2.imshow("Video", canny)
    elif ret == False: break
    if cv2.waitKey(1) & 0XFF == ord('q'): break

Kodu biraz sadeleştirmeye çalıştım, ilk hali çok daha karmaşıktı. Resim boyutunu düşürmenin etkisi olur mu?

zeros_frame[:, :320] = combined_mask[:, :320]
Yavaşlamaya sebep olan kısım yukarıda ekranı böldüğüm kısımmış. combined_mask değeri yerine canny değerini yazınca sorun tamamen çözüldü.
 
Son düzenleme:

Yeni konular

Geri
Yukarı