How to use Opencv to achieve static gesture recognition to play rock paper scissors?

To run the code, make sure you have: python 2.7, opencv 2.4.9

The effect is as follows:

How to use Opencv to achieve static gesture recognition to play rock paper scissors?

How to use Opencv to achieve static gesture recognition to play rock paper scissors?

How to use Opencv to achieve static gesture recognition to play rock paper scissors?

How to use Opencv to achieve static gesture recognition to play rock paper scissors?

How to use Opencv to achieve static gesture recognition to play rock paper scissors?

How to use Opencv to achieve static gesture recognition to play rock paper scissors?

How to use Opencv to achieve static gesture recognition to play rock paper scissors?

The algorithm is as follows:

Process the image first, the process:

1. Obtain the contour by subtracting the swelled image from the etched image.

2. Obtain images with binarization

3. Inverted

After the above processing, the picture is:

This is simple, and design a way to distinguish the three images.

code show as below:

# -*- coding: cp936 -*- import cv2 import numpy import TIme import random import os def judge( ): # construct a 3&TImes;3 structural element # return 0 stone ,1 jiandao, 2 bu img = cv2.imread( "wif.jpg",0) element = cv2.getStructuringElement(cv2.MORPH_RECT,(11,11)) dilate = cv2.dilate(img, element) erode = cv2.erode(img, element) #对两图片相Subtracting the obtained edge, the first parameter is the expanded image, the second parameter is the corrupted image result = cv2.absdiff(dilate, erode); # The result obtained above is the grayscale image, which is binarized so that A clearer observation retval, result = cv2.threshold(result, 40, 255, cv2.THRESH_BINARY); #反色, that is, the inverse of each pixel of the binary image result = cv2.bitwise_not(result); result =cv2 .medianBlur(result,23) a=[] posi =[] width =[] count = 0 area = 0 for i in range(result.shape[1]): for j in range(result.shape[0]) : if(result[j][i]==0): area+=1 for i in range(result.shape[1]): if(result[5*result.shape[0]/16][i]= =0 and result[5*result.shape[0]/16][i-1]!=0 ): count+=1 width.a Ppend(0) posi.append(i) if(result[5*result.shape[0]/16][i]==0): width[count-1]+=1 """ print 'the pic width Is ',result.shape[1],'' for i in range(count): print 'the ',i,'th',' ','is'; print 'width ',width[i] print 'posi ',posi[i],'' print count,'' print 'area is ',area,'' cv2.line(result,(0,5*result.shape[0]/16),(214,5* Result.shape[0]/16),(0,0,0)) cv2.namedWindow("fcuk") cv2.imshow("fcuk",result) cv2.waitKey(0) """ #determine time width_length= 0 width_jiandao = True for i in range(count): if width[i]>45: #print 'bu1'; return 2; if width[i]<=20 or width[i]>=40: width_jiandao= False width_length += width[i] if width_jiandao==True and count==2: return 1; if(area <8500): #print 'shi tou'; return 0; print "width_leng",width_length if(width_length<35): #This time indicates that the photo is down, so you need to re-measure. a=[] posi =[] width =[] count = 0 for i in range(result.shape[1]): if(result[11*result.shape[0]/16][i]==0 and Result[11*result.shape[0]/16][i-1]!=0 ): count+=1 width.append(0) posi.append(i) if(result[11*result.shape[0] /16][i]==0): width[count-1]+=1 """ print 'the pic width is ',result.shape[1],'' for i in range(count): print ' The ',i,'th',' ','is'; print 'width ',width[i] print 'posi ',posi[i],'' print count,'' print 'area is ',area, '' """ width_length=0 width_jiandao = True for i in range(count): if width[i]>45: #print 'bu1'; return 2; if width[i]<=20 or width[i]> =40: width_jiandao= False width_length += width[i] if width_jiandao==True and count==2: return 1; if(area>14000 or count>=3): #print 'bu2'; return 2; if( Width_length<110): #print 'jian dao'; return 1; else: #print 'bu3'; return 2; """ print("This is a rock-paper-scissor game played by the camera, enter y to start") s = raw_input() capture = cv2.VideoCapture(0) cv2.namedWindow("camera",1) start_TIme = TIme.time() print("Give you 5 The time of the second is placed in the position of the box") while(s=='y' or s=='Y'): ha,img =capture.read() end_time = time.time() cv2.rectangle(img ,(426,0),(640,250),(170,170,0)) cv2.putText(img,str(int((5-(end_time- start_time))))), (100,100), cv2.FONT_HERSHEY_SIMPLEX, 2, 255 Cv2.imshow("camera",img) if(end_time-start_time>5): break if(cv2.waitKey(30)>=0): break ha,img = capture.read() capture.release() cv2 .imshow("camera",img) img = img[0:210,426:640] cv2.imwrite("wif.jpg",img) judge() cv2.waitKey(0) print "fuck" """ def game( ): fuck =[] fuck.append("stone") fuck.append("scissors") fuck.append("cloth") capture = cv2.VideoCapture(0) cv2.namedWindow("camera",1) start_time = Time.time() print("Give you 5 seconds to put the handle to the position of the box") while(1): ha,img =capture.read() end_time = time.time() cv2.rectangle(img, (426,0),(640,250),(170,170,0)) cv2.putText(img,str(int((5-(end_time- start_time))))), (100,100), cv2.FONT_HERSHEY_SIMPLEX, 2, 255) Cv2.imshow("camera",img) if(end_time-start_time>5): break if(cv2. waitKey(30)>=0): break ha,img = capture.read() capture.release() cv2.imshow("camera",img) img = img[0:210,426:640] cv2.imwrite("wif .jpg",img) p1 = judge() pc = random.randint(0,2) #print p1,' ',pc,'' print "You are out, "fuck[p1]," the computer is ",fuck[pc],"" cv2.destroyAllWindows() if(p1==pc): print "draw" return 0 if((p1==0 and pc ==1) or(p1==1 and pc = =2)or(p1==2 and pc ==0)): print 'you won' return 1 else: print 'you lost' return -1 def main(): you_win=0 pc_win=0 print(" This is a rock-paper-scissor game played by the camera. Please enter the carriage return to start the game") s = raw_input() while(1): print "score (player: computer)", you_win, ":", pc_win, ' ' s = raw_input() os.system('cls') ans =game() if(ans == 1): you_win+=1 elif(ans == -1): pc_win+=1 print "To reduce false positives, please As much as possible, occupy the largest possible frame "main()

TFT LCD Arduino

TFT LCD Arduino

TFT LCD Arduino,Active Matrix,LCD Module

Shenzhen Newvision Technology Co.Ltd , https://www.newvisionlcd.com

Posted on