深入浅出,自动化视觉跟踪实战项目
以下文章来源于微信公众号:小白学视觉
作者:Marcelo Rovai
本文仅用于学术分享,如有侵权,请联系后台作删文处理
导语
安装 OpenCV 3库
source ~/.profile
workon cv
(cv) pi@raspberry:~$
python
import cv2
测试相机
既然你的树莓派已经安装好OpenCV了,那就先测试一下你的相机是否正常工作吧~(假设你已经在你的树莓派上安装PiCam了)
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
frame = cv2.flip(frame, -1) # Flip camera vertically
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow( frame , frame)
cv2.imshow( gray , gray)
if cv2.waitKey(1) & 0xFF == ord( q ):
breakcap.release()
cv2.destroyAllWindows()
pythonsimpleCamTest.py
使用 Python 与 OpenCV 进行颜色检测
- 蓝色:71
- 绿色:234
- 红色:213
import sys
import numpy as np
import cv2
blue = sys.argv[1]
green = sys.argv[2]
red = sys.argv[3]
color = np.uint8([[[blue, green, red]]])
hsv_color = cv2.cvtColor(color, cv2.COLOR_BGR2HSV)
hue = hsv_color[0][0][0]
print("Lower bound is :"),
print("[" + str(hue-10) + ", 100, 100]")
print("Upper bound is :"),
print("[" + str(hue + 10) + ", 255, 255]")
pythonbgr_hsv_converter.py 71 234 213
lower bound:
upper bound:
import cv2
import numpy as np
# Read the picure - The 1 means we want the image in BGR
img = cv2.imread( yellow_object.JPG , 1)
# resize imag to 20% in each axis
img = cv2.resize(img, (0,0), fx=0.2, fy=0.2)
# convert BGR image to a HSV image
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# NumPy to create arrays to hold lower and upper range
# The “dtype = np.uint8” means that data type is an 8 bit integer
lower_range = np.array([24, 100, 100], dtype=np.uint8)
upper_range = np.array([44, 255, 255], dtype=np.uint8)
# create a mask for image
mask = cv2.inRange(hsv, lower_range, upper_range)
# display both the mask and the image side-by-side
cv2.imshow( mask ,mask)
cv2.imshow( image , img)
# wait to user to press [ ESC ]
while(1):
k = cv2.waitKey(0)
if(k == 27):
breakcv2.destroyAllWindows()
pythoncolorDetection.py
移动物体跟踪
pip install imutils
pythonball_traking.py
frame = imutils.rotate(frame, angle=180)
测试通用IO
pipinstallRPi.GPIO
import sys
import time
import RPi.GPIO as GPIO
# initialize GPIO and variables
redLed = int(sys.argv[1])
freq = int(sys.argv[2])
GPIO.setmode(GPIO.BCM)
GPIO.setup(redLed, GPIO.OUT)
GPIO.setwarnings(False)
print(" [INFO] Blinking LED (5 times) connected at GPIO {0}
at every {1} second(s)".format(redLed, freq))
for i in range(5):
GPIO.output(redLed, GPIO.LOW)
time.sleep(freq)
GPIO.output(redLed, GPIO.HIGH)
time.sleep(freq)
# do a bit of cleanup
print(" [INFO] Exiting Program and cleanup stuff ")
GPIO.cleanup()
pythonLED_simple_test.py 21 1
识别颜色和GPIO交互
import RPi.GPIO as GPIO
redLed = 21
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(redLed, GPIO.OUT)
GPIO.output(redLed, GPIO.LOW)
ledOn = False
GPIO.output(redLed, GPIO.HIGH)
ledOn = True
pythonobject_detection_LED.py
云台机制
现在我们已经用上了基本的 OpenCV 和 GPIO,那么接下来我们升级一下云台机制。
获取更多细节,请查看我的教程:Pan-Tilt-Multi-Servo-Control
- GPIO 17 ==> 倾斜伺服
- GPIO 27 ==> 水平伺服
from time import sleep
import RPi.GPIO as GPIO)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)defsetServoAngle(servo, angle):
pwm = GPIO.PWM(servo, 50)
pwm.start(8)
dutyCycle = angle / 18. + 3.
pwm.ChangeDutyCycle(dutyCycle)
sleep(0.3)
pwm.stop()
if __name__ == __main__ :
import sys
servo = int(sys.argv[1])
GPIO.setup(servo, GPIO.OUT)
setServoAngle(servo, int(sys.argv[2]))
GPIO.cleanup()
pythonangleServoCtrl.py 17 45
实时地获取到物体位置
# only proceed if the radius meets a minimum size
if radius > 10:
# draw the circle and centroid on the frame,
# then update the list of tracked points
cv2.circle(frame, (int(x), int(y)), int(radius),
(0, 255, 255), 2)
cv2.circle(frame, center, 5, (0, 0, 255), -1)
# print center of circle coordinates
mapObjectPosition(int(x), int(y))
# if the led is not already on, turn the LED on
ifnot ledOn:
GPIO.output(redLed, GPIO.HIGH)
ledOn = True
defmapObjectPosition(x, y):
print ("[INFO] Object Center coordinates at
X0 = {0} and Y0 = {1}".format(x, y))
物体位置追踪系统
- 220 < x < 280
- 160 < y < 210
# position servos to present object at center of the frame
defmapServoPosition (x, y):
globalpanAngle
globaltiltAngle
if(x < 220):
panAngle+= 10
ifpanAngle > 140:
panAngle = 140
positionServo(panServo, panAngle)
if(x > 280):
panAngle-= 10
ifpanAngle < 40:
panAngle = 40
positionServo(panServo, panAngle)
if(y < 160):
tiltAngle+= 10
iftiltAngle > 140:
tiltAngle = 140
positionServo(tiltServo, tiltAngle)
if(y > 210):
tiltAngle-= 10
iftiltAngle < 40:
tiltAngle = 40
positionServo(tiltServo, tiltAngle)
defpositionServo(servo, angle):
os.system("python angleServoCtrl.py " + str(servo) + " " +
str(angle))
print("[INFO] Positioning servo at GPIO {0} to {1}
degrees".format(servo, angle))
结论
推荐阅读
AI融资 | Aidlux智能物联网公司阿加犀获得高通融资
AI融资 | Aidlux智能物联网公司阿加犀获得高通融资
Yolov5应用 | 家庭安防告警系统全流程及代码讲解 江大白 | 这些年从0转行AI行业的一些感悟 白夜 | 一文看懂AI项目流程及边缘设备开发
Yolov5应用 | 家庭安防告警系统全流程及代码讲解
江大白 | 这些年从0转行AI行业的一些感悟
白夜 | 一文看懂AI项目流程及边缘设备开发
人工智能行业,研究方向很多,大大小小有
几十个方向。
© THE END
大家一起加油!
阅读原文 最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。