1) The Python program need extra libraries to work like Opencv, numpy etc.
Refer this link to install them (Works perfectly in Ubuntu / Debian OS)
Link: http://docs.opencv.org/doc/tutorials/introduction/linux_install/linux_install.html
Refer this link to install them (Works perfectly in Ubuntu / Debian OS)
Link: http://docs.opencv.org/doc/tutorials/introduction/linux_install/linux_install.html
2) The program output data like “move STIRO right, left etc”. You need to out some values corresponding to this output through IO pins in Beaglebone Black.
Eg: moving forward = 010
moving right = 001
moving left = 100
pick ball = 111
Refer this PDF for setting and outing values through IO pins
Link: https://learn.adafruit.com/downloads/pdf/setting-up-io-python-library-on-beaglebone-black.pdf
Link: https://learn.adafruit.com/downloads/pdf/setting-up-io-python-library-on-beaglebone-black.pdf
Program
Note: STI-RO is my robot name
Note: STI-RO is my robot name
import numpy as np
import cv2
import time
cap =cv2.VideoCapture(1)
j=0
i=0
while(True):
j=(j+1)
ret, frame=cap.read()
output=frame
hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
lower_blue=np.array([40,50,20])
upper_blue=np.array([100,240,240])
mask=cv2.inRange(hsv,lower_blue,upper_blue)
res=cv2.bitwise_and(frame,frame,mask=mask)
erode=cv2.erode(mask,None,iterations=5)
dilate=cv2.dilate(erode,None,iterations=5)
contours,hierarchy=cv2.findContours(erode,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
nw=0
nh=0
nx=0
ny=0
ncnt=0
i=0
for cnt in contours:
x,y,w,h=cv2.boundingRect(cnt)
if 1.2>w/h>0.8 or 1.2>h/w>0.8:
if nw<w:
ncnt=cnt
nw=w
nh=h
nx=x
ny=y
i=(i+1)
if (i!=0) and (nw>70):
leftmost = (ncnt[ncnt[:,:,0].argmin()][0])
rightmost = (ncnt[ncnt[:,:,0].argmax()][0])
topmost = (ncnt[ncnt[:,:,1].argmin()][0])
bottommost = (ncnt[ncnt[:,:,1].argmax()][0])
moments = cv2.moments(ncnt)
yc = moments['m01'] / moments['m00']
xc = moments['m10'] / moments['m00']
x_diff=((topmost[0:-1])-xc)
y_diff=((leftmost[1:])-yc)
if (-35<x_diff<15) and (-35<y_diff<15) and (-15<(nw-nh)<15):
print 'diff= ',nw-nh
print 'CONCLUSIONS FROM THE OBTAINED DATA'
print 'BALL FOUND'
print 'width in pixels= ',nw
if (300<xc<340):
print 'Distance form center= ',320-xc
print 'The ball is at center'
print '.................................'
if (260<nw<300):
print 'The ball is in capture range of 20cm'
print 'PICK THE BALL...!'
elif (nw<260):
print 'The ball far away from capture range'
print 'Move STI-RO forward'
else:
print 'The ball is more nearer than capture range'
print 'Move STI-RO backward'
elif (xc>340):
print 'Distance from center= ',320-xc,'pixels'
print 'The ball is in right side'
print 'Turn STI-RO right'
print '.................................'
print '.................................'
else:
print 'Distance from center= ',320-xc,'pixels'
print 'The ball is in left side'
print 'Turn STI-RO left'
print '.................................'
print '.................................'
else:
print 'diff= ',nw-nh
print 'CONCLUSIONS FROM THE OBTAINED DATA'
print 'BALL NOT FOUND'
print 'CONTINUE SEARCHING FOR BALL'
print '.................................'
print '.................................'
print '.................................'
print '.................................'
print '.................................'
print '____________________________________________________'
cv2.line(output,(310,200),(310,280),(0,255,0),5) #range left
cv2.line(output,(330,200),(330,280),(0,255,0),5) #range right
cv2.line(output,(180,140),(180,340),(255,0,0),15) #range capture
cv2.line(output,(460,140),(460,340),(255,0,0),15) #range capture
cv2.rectangle(output,(nx,ny),(nx+nw,ny+nh),[0,255,255],2) #box
cv2.line(output,(nx+nw/2,0),(nx+nw/2,480),(0,0,255),2) #target x
cv2.line(output,(0,ny+nh/2),(640,ny+nh/2),(0,0,255),2) #target y
cv2.imshow('output',output)
cv2.imshow('mask', mask)
cv2.imshow('res',res)
cv2.imshow('erode',erode)
cv2.imshow('dilatte',dilate)
if cv2.waitKey(1) &0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
No comments:
Post a Comment