摄像头运行过程中出现画面卡顿,应该怎么解决?
-
import sensor, image from pid import PID import pyb import json sensor.reset() sensor.set_pixformat(sensor.GRAYSCALE) sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(100) sensor.set_vflip(True) sensor.set_hmirror(True) init_ok=True fan=0 ok=0 spin=0 count_started=0 circles=[] size_threshold=1100 x_pid = PID(p=0.5, i=1, imax=100) h_pid = PID(p=0.05, i=0.1, imax=50) uart = pyb.UART(3, 9600) #找视野中最大的圆 def find_max_circle(circles): max_r=0 for c in circles: if 5**c.r()>5**max_r: max_circle=c max_r=c.r() return max_circle #判断是否开始接收 while(not init_ok): if uart.readline()=='Init_ok!': init_ok=True while(True): while(fan): if uart.readline()=='get!': break fan=0 img = sensor.snapshot().lens_corr(1.8) circles.clear() all_circles=img.find_circles(threshold = 7500, x_margin = 10, y_margin = 10, r_margin = 10, r_min = 2, r_max = 120) for c in all_circles: if c.r()<22: circles.append(c) if circles: if spin: count_stop() spin=0 max_circle = find_max_circle(circles) x_error = max_circle.x()-img.width()/2 h_error = 4*(max_circle.r()**2)-size_threshold img.draw_circle(max_circle.x(),max_circle.y(),max_circle.r()) img.draw_cross(max_circle.x(), max_circle.y()) x_output=x_pid.get_pid(x_error,1) h_output=h_pid.get_pid(h_error,1) if -250<h_error<250 and -25<x_error<25: ok+=1 if ok>=2: fan=1 ok=0 leftpwm=-h_output-x_output rightpwm=-h_output+x_output obj = {"chase":{"leftpwm" : leftpwm ,"rightpwm" : rightpwm}, "fan":fan, "spin":spin} output='$'+json.dumps(obj)+'%' uart.write(output) else: pass
-
麻烦您了,麻烦您了!
-
你的代码我运行不起来,count_stop()是什么?
你应该把没用的逻辑删掉,只留重要的部分,测试一下效果。
比如把串口处理的部分删掉。
-
import sensor, image
from pid import PID
import pybsensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(100)
sensor.set_vflip(True)
sensor.set_hmirror(True)circles=[]
#找视野中最大的圆
def find_max_circle(circles):
max_r=0
for c in circles:
if 5c.r()>5max_r:
max_circle=c
max_r=c.r()
return max_circlewhile(True):
img = sensor.snapshot().lens_corr(1.8)
circles.clear()
all_circles=img.find_circles(threshold = 750,
x_margin = 10, y_margin = 10, r_margin = 10,
r_min = 2, r_max = 120)for c in all_circles: if c.r()<22: circles.append(c) if circles: max_circle = find_max_circle(circles) img.draw_circle(max_circle.x(),max_circle.y(),max_circle.r()) img.draw_cross(max_circle.x(), max_circle.y()) else: pass
-
@kidswong999
感谢您的帮助。这份精简版的代码仅保留了寻找视野中最大圆,然后画圈圈住的功能。麻烦你再运行一下。
-
我运行下面的代码,一直都是7帧到8帧。并没有看到卡顿的情况。
import sensor, image import pyb import time sensor.reset() sensor.set_pixformat(sensor.GRAYSCALE) sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(100) sensor.set_vflip(True) sensor.set_hmirror(True) clock = time.clock() circles=[] #找视野中最大的圆 def find_max_circle(circles): max_r=0 for c in circles: if 5**c.r()>5**max_r: max_circle=c max_r=c.r() return max_circle while(True): clock.tick() img = sensor.snapshot().lens_corr(1.8) circles.clear() all_circles=img.find_circles(threshold = 750, x_margin = 10, y_margin = 10, r_margin = 10, r_min = 2, r_max = 120) for c in all_circles: if c.r()<22: circles.append(c) if circles: max_circle = find_max_circle(circles) img.draw_circle(max_circle.x(),max_circle.y(),max_circle.r()) img.draw_cross(max_circle.x(), max_circle.y()) else: pass print(clock.fps())
-
@kidswong999
您好,我们现在遇到的具体情况是这样的。目前程序中只加载了PID算法和寻找视野中最大球的模块。视野内图像相对复杂时会出现死机(图像不动,编译器显示代码仍在运行)的情况。一旦视野内图像稍微复杂(例如将手伸进去)立刻死机。在简单视野长时间运行测试后,会出现一运行程序就死机的现象。
请问这种性能严重不稳定的情况如何解决呢?
另外,如果在你的摄像头上运行正常的话,请把你的摄像头型号告诉我好吗?我们的摄像头是open MV3。
-
@kidswong999
这是代码:
import sensor, image, time
from pid import PID
import pybsensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(100)
sensor.set_vflip(True)
sensor.set_hmirror(True)ok=0
circles=[]
size_threshold=500 # 保持距离的像素阈值
x_pid = PID(p=0.5, i=1, imax=100)
h_pid = PID(p=0.05, i=0.1, imax=50)
uart = pyb.UART(3, 9600)#找视野中最大的圆
def find_max_circle(circles):
max_r=0
for c in circles:
if 5c.r()>5max_r:
max_circle=c
max_r=c.r()
return max_circlewhile(True):
img = sensor.snapshot().lens_corr(1.8)
circles.clear()
all_circles=img.find_circles(threshold = 2000,
x_margin = 10, y_margin = 10, r_margin = 10,
r_min = 2, r_max = 120)for c in all_circles: if c.r()<22: circles.append(c) if circles: max_circle = find_max_circle(circles) x_error = max_circle.x()-img.width()/2 h_error = 3.14*(max_circle.r()**2)-size_threshold img.draw_circle(max_circle.x(),max_circle.y(),max_circle.r(),color = (255, 0, 0)) img.draw_cross(max_circle.x(), max_circle.y(),color = (255, 0, 0)) x_output=x_pid.get_pid(x_error,1)*0.05 h_output=h_pid.get_pid(h_error,1)*0.2 if -250<h_error<250 and -25<x_error<25: ok+=1 if ok>=2: fan=1 ok=0 print("h_error:",h_error ,"h_output:",h_output,"x_output:",x_output) else: pass
-
@kidswong999 您好,请问我们这个问题除了改变算法之外还有解决余地吗?
-
您好,问题已解决了,是我们的代码陷入了某个死循环。谢谢您的帮助。