1+ import cv2
2+ import numpy as np
3+ import pickle , os , sqlite3 , random
4+
5+ image_x , image_y = 50 , 50
6+
7+ def get_hand_hist ():
8+ with open ("hist" , "rb" ) as f :
9+ hist = pickle .load (f )
10+ return hist
11+
12+ def init_create_folder_database ():
13+ # create the folder and database if not exist
14+ if not os .path .exists ("gestures" ):
15+ os .mkdir ("gestures" )
16+ if not os .path .exists ("gesture_db.db" ):
17+ conn = sqlite3 .connect ("gesture_db.db" )
18+ create_table_cmd = "CREATE TABLE gesture ( g_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, g_name TEXT NOT NULL )"
19+ conn .execute (create_table_cmd )
20+ conn .commit ()
21+
22+ def create_folder (folder_name ):
23+ if not os .path .exists (folder_name ):
24+ os .mkdir (folder_name )
25+
26+ def store_in_db (g_id , g_name ):
27+ conn = sqlite3 .connect ("gesture_db.db" )
28+ cmd = "INSERT INTO gesture (g_id, g_name) VALUES (%s, \' %s\' )" % (g_id , g_name )
29+ try :
30+ conn .execute (cmd )
31+ except sqlite3 .IntegrityError :
32+ choice = input ("g_id already exists. Want to change the record? (y/n): " )
33+ if choice .lower () == 'y' :
34+ cmd = "UPDATE gesture SET g_name = \' %s\' WHERE g_id = %s" % (g_name , g_id )
35+ conn .execute (cmd )
36+ else :
37+ print ("Doing nothing..." )
38+ return
39+ conn .commit ()
40+
41+ def store_images (g_id ):
42+ total_pics = 1200
43+ hist = get_hand_hist ()
44+ cam = cv2 .VideoCapture (1 )
45+ if cam .read ()[0 ]== False :
46+ cam = cv2 .VideoCapture (0 )
47+ x , y , w , h = 300 , 100 , 300 , 300
48+
49+ create_folder ("gestures/" + str (g_id ))
50+ pic_no = 0
51+ flag_start_capturing = False
52+ frames = 0
53+
54+ while True :
55+ img = cam .read ()[1 ]
56+ img = cv2 .flip (img , 1 )
57+ imgHSV = cv2 .cvtColor (img , cv2 .COLOR_BGR2HSV )
58+ dst = cv2 .calcBackProject ([imgHSV ], [0 , 1 ], hist , [0 , 180 , 0 , 256 ], 1 )
59+ disc = cv2 .getStructuringElement (cv2 .MORPH_ELLIPSE ,(10 ,10 ))
60+ cv2 .filter2D (dst ,- 1 ,disc ,dst )
61+ blur = cv2 .GaussianBlur (dst , (11 ,11 ), 0 )
62+ blur = cv2 .medianBlur (blur , 15 )
63+ thresh = cv2 .threshold (blur ,0 ,255 ,cv2 .THRESH_BINARY + cv2 .THRESH_OTSU )[1 ]
64+ thresh = cv2 .merge ((thresh ,thresh ,thresh ))
65+ thresh = cv2 .cvtColor (thresh , cv2 .COLOR_BGR2GRAY )
66+ thresh = thresh [y :y + h , x :x + w ]
67+ contours = cv2 .findContours (thresh .copy (), cv2 .RETR_TREE , cv2 .CHAIN_APPROX_NONE )[1 ]
68+
69+ if len (contours ) > 0 :
70+ contour = max (contours , key = cv2 .contourArea )
71+ if cv2 .contourArea (contour ) > 10000 and frames > 50 :
72+ x1 , y1 , w1 , h1 = cv2 .boundingRect (contour )
73+ pic_no += 1
74+ save_img = thresh [y1 :y1 + h1 , x1 :x1 + w1 ]
75+ if w1 > h1 :
76+ save_img = cv2 .copyMakeBorder (save_img , int ((w1 - h1 )/ 2 ) , int ((w1 - h1 )/ 2 ) , 0 , 0 , cv2 .BORDER_CONSTANT , (0 , 0 , 0 ))
77+ elif h1 > w1 :
78+ save_img = cv2 .copyMakeBorder (save_img , 0 , 0 , int ((h1 - w1 )/ 2 ) , int ((h1 - w1 )/ 2 ) , cv2 .BORDER_CONSTANT , (0 , 0 , 0 ))
79+ save_img = cv2 .resize (save_img , (image_x , image_y ))
80+ rand = random .randint (0 , 10 )
81+ if rand % 2 == 0 :
82+ save_img = cv2 .flip (save_img , 1 )
83+ cv2 .putText (img , "Capturing..." , (30 , 60 ), cv2 .FONT_HERSHEY_TRIPLEX , 2 , (127 , 255 , 255 ))
84+ cv2 .imwrite ("gestures/" + str (g_id )+ "/" + str (pic_no )+ ".jpg" , save_img )
85+
86+ cv2 .rectangle (img , (x ,y ), (x + w , y + h ), (0 ,255 ,0 ), 2 )
87+ cv2 .putText (img , str (pic_no ), (30 , 400 ), cv2 .FONT_HERSHEY_TRIPLEX , 1.5 , (127 , 127 , 255 ))
88+ cv2 .imshow ("Capturing gesture" , img )
89+ cv2 .imshow ("thresh" , thresh )
90+ keypress = cv2 .waitKey (1 )
91+ if keypress == ord ('c' ):
92+ if flag_start_capturing == False :
93+ flag_start_capturing = True
94+ else :
95+ flag_start_capturing = False
96+ frames = 0
97+ if flag_start_capturing == True :
98+ frames += 1
99+ if pic_no == total_pics :
100+ break
101+
102+ init_create_folder_database ()
103+ g_id = input ("Enter gesture no.: " )
104+ g_name = input ("Enter gesture name/text: " )
105+ store_in_db (g_id , g_name )
106+ store_images (g_id )
0 commit comments