Python and Arduino serial communication docking OPENCV for intelligent item sorting

Source: Internet
Author: User

2018-05-0118:53:50

First:

Video again:

http://v.youku.com/v_show/id_XMzU3NzAwNzMyNA==.html?spm=a2hzp.8244740.0.0

#Coding=utf-8 fromPyfirmataImportArduino, UtilImport TimeImportCv2ImportNumPy as NPImportSerialser=Serial. Serial () ser.baudrate= 9600#set baud rateSer.port ='COM6'  #Port is COM3Print(Ser) ser.open ()#Open the serial portPrint(Ser.is_open)#Verify that the serial port is open#board = Arduino (' COM3 ')"""def Duoji (): board.servo_config (0, 255,) print ("Ceshi") time.sleep (0.2) board.servo_config (13, 0, 255, 255) Time.sleep (0.2) def Arduino (): Board.digital[13].write (0) # write to IO port 13 0 time.sleep (0.1) Board.digital [13].write (1) # to IO Port 13 write 1 Time.sleep (0.1) Board.analog [13].write]"""defDetect_circle_demo (): Video_capture=Cv2. Videocapture (0) whileTrue:if  notvideo_capture.isopened ():Print('unable to load camera.')             Breakret, img=Video_capture.read ()#img = cv2.pyrmeanshiftfiltering (IMG, ten, +)Gray = Cv2.cvtcolor (img, Cv2. Color_bgr2gray)#Grayscale ImageCircles1 = Cv2. Houghcircles (Gray, Cv2. Hough_gradient, 1, param1=100, param2=100, minradius=50,maxradius=200)       #CV. Houghcircles (CImage, CV. Hough_gradient, 1, param1=50, param2=30, Minradius=0, maxradius=0)        Try:#If the previous step is not detected. Execution of the try content will cause an error. can be modified to try to look under. Circles = circles1[0,:,:]#extracted as two-dimensional        exceptTypeError:Print('no circular objects found!! ')        Else: Circles= Np.uint16 (Np.around (circles))#Rounding             forIinchcircles[:]: Cv2.circle (IMG, (i[0), i[1]), i[2], color=[0, 0, 0], thickness=2)#Draw a circleCv2.circle (IMG, (i[0], i[1]), 2, color=[0, 255, 0], thickness=2)#Draw CenterCv2.puttext (IMG,"Center", (I[0]-i[1), Cv2. Font_hershey_simplex, 0.5, (255, 255, 255), 2)                Print(i[0],i[1])                Print("A circular object is detected and the separation begins! ") Ser.write (b"a")                #Print (Ser.read (1))                #Output coordinates        #Show VideoCv2.imshow ('Video', IMG) cv2.waitkey (10) Detect_circle_demo ()"""DP, which detects the inverse of the resolution of the center of the accumulator image to the ratio of the input image, and this parameter allows the creation of an accumulator with a lower resolution than the input image. If the above words are difficult to understand, let's look at examples. For example, if dp= 1 o'clock, the accumulator and the input image have the same resolution. If dp=2, the accumulator has a width and height that is half as large as the input image. Mindist, the minimum distance between the center of the circle detected by the Hough transform, the minimum distance between the two different circles that allow our algorithm to differentiate clearly. If this parameter is too small, multiple adjacent circles may be incorrectly detected as a coincident circle. Conversely, if this parameter is set too large, some circles cannot be detected. param1, with a default value of 100. It is the corresponding parameter of the method set for the detection. For the current only method, the Hough gradient method, which represents the high threshold value passed to the canny edge detection operator, and the low threshold is half the high threshold value. Param2, there is also a default value of 100. It is the corresponding parameter of the method set for the detection. The current only method of the Hough gradient method, which represents the accumulator threshold at the center of the detection phase. The smaller it is, the more it can detect a circle that doesn't exist, and the bigger it is, the more rounded it is to be able to pass through the circle. Minradius, the default value of 0, represents the minimum value of the circle radius. Maxradius, also has a default value of 0, which indicates the maximum value of the circle radius"""

The principle of the program is image recognition, and the round object is taken out. Python sends the data to the microcontroller, and the rudder performs the relevant action.

#include <Stepper.h>#include<Servo.h>Servo Myservo; Servo Myservo1; Charserial_line[ -] ="";intSerial_line_length=0; CharVal='/'; #defineSTEPS 100Stepper Stepper (STEPS,8,9,Ten, One); intPos;intpos1;inti;voidSetup () {Serial.begin (9600); Pinmode (5, OUTPUT); Digitalwrite (5, high); Myservo.attach (3); Myservo1.attach (4); Myservo.write ( Max); Myservo1.write ( -); Stepper.setspeed ( -); } voidzhixing () {Myservo.write ( -);//RightDelay -); Digitalwrite (5, low);//Suction     for(pos = -;p os>= the;p os-=1) {myservo1.write (POS); Delay ( the); } myservo1.write ( -); Delay ( -); Myservo.write ( Max); /////Return positionDelay +); Digitalwrite (5, high); }voidLoop () {Stepper.step (-1); Serial.println ("Go"); Val=Serial.read (); if(val = ='a')//a circular object was detected{  if(i = = -) {serial.println ('6');  Zhixing (); I=0 ; }    Elsei++;//serial.println (i);    }  }

The two cooperate with each other to achieve the object sorting.

Python and Arduino serial communication docking OPENCV for intelligent item sorting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.