影片是我Demo的操作影片
這套系統硬體架構是長這樣的

由網路相機(webCamera)將影像訊息讀入PC,解出設定好的動作輸出。
在程式軟體上的架構是長這樣的

視覺算法的部分:
使用的是開源電腦視覺庫 OpenCV
這套庫在各大程式語言都有函式庫能使用,在此我使用的是Python

這篇有教學如何用OpenCV讀入 WebCamera的圖像
擷取網路攝影機串流影像
讀入影像後,做的動作流程是

我把視覺辨識部分寫成一個副程式 稱作CVJOB,之後引用它就會是跑這個流程。
import cv2target=[0,0]detect_state=False
def CVJOB():
global target,detect_state
detect_state =True
cap = cv2.VideoCapture(0)
itt = 0
while (True):
ret, frame = cap.read()
im = cv2.resize(frame, (500, 500), interpolation=cv2.INTER_CUBIC)
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (7, 7), 0)
low_threshold = 1
high_threshold = 10
edges = cv2.Canny(blurred, low_threshold, high_threshold)
contours, hierachy = cv2.findContours(edges, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
cnt_count = []
centerX = []
centerY = []
for cnt in range(len(contours)):
epsilon = 0.04 * cv2.arcLength(contours[cnt], True)
approx = cv2.approxPolyDP(contours[cnt], epsilon, True)
area = cv2.contourArea(contours[cnt])
if (len(approx) < 5 and len(approx) > 3 and area > 2000 and area < 4000):
cv2.drawContours(im, contours[cnt], -1, (255, 255, 255), 3)
M = cv2.moments(contours[cnt])
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
cnt_count.append(cnt)
centerX.append(cX)
centerY.append(cY)
f1 = int(cY * 0.541 + 48.602)
f2 = int(cX * 0.571 - 93.28)
target[0]=f1
target[1]=f2
text = str( f1) + ',' + str( f2 )
cv2.putText(im, text, (50, 50), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 255, 255), 1, cv2.LINE_AA)
cv2.circle(im, (cX, cY), 10, (1, 227, 254), -1)
cv2.imshow('123', im)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
elif cv2.waitKey(1) & 0xFF == ord('s'): # 按s存檔
itt = itt + 1;
cap.release()
cv2.destroyAllWindows()
cv2.waitKey(1)
print('Detect')