def hand_pose(model,move_text): #Camera and font initialization cap=cv2.VideoCapture(1) array=[] array1=[] #The main event loop while(cap.isOpened()): move='' _,img=cap.read() gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret,th1 = cv2.threshold(gray.copy(),100,255,cv2.THRESH_TOZERO) _,contours,hierarchy = cv2.findContours(th1.copy(),cv2.RETR_EXTERNAL, 2) cnt=ut.getMaxContour(contours,4000) if cnt!=None: gesture,res=ut.getGestureImg(cnt,img,th1,model) cv2.imshow('PredictedGesture',gesture) move=move_text[res] array.append(move) if len(array)==30: array1.append(move) print move if len(array1)>=5: a=''.join(array1) tts.TTS(a) array1=[] array=[] cv2.imshow('Frame',img) k = 0xFF & cv2.waitKey(10) if k == 27: break cap.release() cv2.destroyAllWindows()
def process(self, img): cv2.imshow('original_image', img) # _,img=cap.read() # cv2.rectangle(img,(900,100),(1300,500),(255,0,0),3) # bounding box which captures ASL sign to be detected by the system # img1=img[100:500,900:1300] img_ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB) blur = cv2.GaussianBlur(img_ycrcb, (11, 11), 0) skin_ycrcb_min = np.array((0, 138, 67)) skin_ycrcb_max = np.array((255, 173, 133)) mask = cv2.inRange( blur, skin_ycrcb_min, skin_ycrcb_max ) # detecting the hand in the bounding box using skin detection contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, 2) cnt = ut.getMaxContour( contours, 4000 ) # using contours to capture the skin filtered image of the hand if cnt != None: gesture, label = ut.getGestureImg( cnt, img, mask, model, word_map, FORMAT ) # passing the trained model for prediction and fetching the result if (label != None): if (self.temp == 0): previouslabel = label if previouslabel == label: previouslabel = label self.temp += 1 else: temp = 0 if (temp == STABILIZATION_QTY): # if(label=='P'): # label=" " # text= text + label # if(label=='Q'): # words = re.split(" +",text) # words.pop() # text = " ".join(words) #text=previousText # cv2.imshow('PredictedGesture',gesture) # showing the best match or prediction # cv2.putText(img,label,(50,150), font,8,(0,125,155),2) # displaying the predicted letter on the main screen # cv2.putText(img,text,(50,450), font,3,(0,0,255),2) print label self.sendMessage(label)
def process(self,img): cv2.imshow('original_image',img) # _,img=cap.read() # cv2.rectangle(img,(900,100),(1300,500),(255,0,0),3) # bounding box which captures ASL sign to be detected by the system # img1=img[100:500,900:1300] img_ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB) blur = cv2.GaussianBlur(img_ycrcb,(11,11),0) skin_ycrcb_min = np.array((0, 138, 67)) skin_ycrcb_max = np.array((255, 173, 133)) mask = cv2.inRange(blur, skin_ycrcb_min, skin_ycrcb_max) # detecting the hand in the bounding box using skin detection contours,hierarchy = cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL, 2) cnt=ut.getMaxContour(contours,4000) # using contours to capture the skin filtered image of the hand if cnt!=None: gesture,label=ut.getGestureImg(cnt,img,mask,model,word_map,FORMAT) # passing the trained model for prediction and fetching the result if(label!=None): if(self.temp==0): previouslabel=label if previouslabel == label: previouslabel=label self.temp+=1 else : temp=0 if(temp==STABILIZATION_QTY): # if(label=='P'): # label=" " # text= text + label # if(label=='Q'): # words = re.split(" +",text) # words.pop() # text = " ".join(words) #text=previousText # cv2.imshow('PredictedGesture',gesture) # showing the best match or prediction # cv2.putText(img,label,(50,150), font,8,(0,125,155),2) # displaying the predicted letter on the main screen # cv2.putText(img,text,(50,450), font,3,(0,0,255),2) print label self.sendMessage(label)
cv2.COLOR_BGR2YCR_CB) # color format settings blur = cv2.GaussianBlur( img_ycrcb, (11, 11), 0 ) # Gaussian blur added to screen helps deal with frams quality and performance skin_ycrcb_min = np.array((0, 138, 67)) # color spaces min skin_ycrcb_max = np.array((255, 173, 133)) # color spaces max mask = cv2.inRange( blur, skin_ycrcb_min, skin_ycrcb_max ) # detecting the hand in the bounding box using skin detection contours, hierarchy = cv2.findContours( mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # track motion points cnt = ut.getMaxContour(contours, 4000) #store total motion points if cnt is not None: # is there something there? gesture, label = ut.getGestureImg( cnt, img1, mask, model ) # passing the trained model for prediction and fetching the result if ( label != None ): # is the model finding something in the box that matches a label? if (temp == 0): # if temp is 0 previouslabel = label # last label now label elif previouslabel == label: # still same gesture? previouslabel = label # track again temp += 1 # increse temp gesture count else: # otherwise temp = 0 # temp gesture back to 0 if (temp == 40): # has it been on screen for 40 temp counts? if (label == 'P'): # create sentence space if user does P label = " " # lable space text = text + label # add results to text
cap = cv2.VideoCapture(cam) font = cv2.FONT_HERSHEY_SIMPLEX #The main event loop while (cap.isOpened()): t = time.time() _, img = cap.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, th1 = cv2.threshold(gray.copy(), 130, 255, cv2.THRESH_BINARY) th1 = cv2.erode(th1, kernel, iterations=2) cv2.imshow('thresh', th1) _, contours, hierarchy = cv2.findContours(th1.copy(), cv2.RETR_EXTERNAL, 2) cnt = ut.getMaxContour(contours, 4000) if cnt != None: gesture, res = ut.getGestureImg(cnt, img, th1, model) cv2.imshow('PredictedGesture', cv2.imread('TrainData2/' + res + '_1.jpg')) M = cv2.moments(cnt) cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) cv2.circle(img, (cx, cy), 5, [0, 0, 255], -1) #cv2.putText(img,res,(x,y), font,1,(255,255,255),2,cv2.LINE_AA) hull = cv2.convexHull(cnt, returnPoints=False) defects = cv2.convexityDefects(cnt, hull) if defects != None: for i in range(defects.shape[0]): s, e, f, d = defects[i, 0] start = tuple(cnt[s][0]) end = tuple(cnt[e][0]) far = tuple(cnt[f][0])
def camera(): print("Opening ASL Model!") try: direct = cwd + "/ASLClassifier.dat" print(direct) model = st.load(direct) print("SVM Loaded successfully..") except: model = st.trainSVM(17) #cap=cv2.VideoCapture(0) #cap=PiCamera(0) camera = PiCamera() camera.resolution = (640, 480) camera.framerate = 32 rawCapture = PiRGBArray(camera, size=(640, 480)) time.sleep(0.1) #rgbFrame = PiRGBArray(camera, size = camera.resolution) #frame1 = captureProcessFrame(camera, rgbFrame, 5) #frameCount = 0 font = cv2.FONT_HERSHEY_SIMPLEX text = " " temp = 0 previouslabel = None # Past label previousText = " " # Past text label = None # current label for frame in camera.capture_continuous( rawCapture, format="bgr", use_video_port=True): # Capture pi camera frames stream = frame.array # store frame input as array rawCapture.truncate(0) # take optional size img = stream # store stream as temp img cv2.rectangle(img, (300, 300), (100, 100), (0, 255, 0), 0) # create rectangle one screen img1 = img[100:300, 100:300] # image stream ratio img_ycrcb = cv2.cvtColor(img1, cv2.COLOR_BGR2YCR_CB) # color format settings blur = cv2.GaussianBlur( img_ycrcb, (11, 11), 0 ) # Gaussian blur added to screen helps deal with frams quality and performance skin_ycrcb_min = np.array((0, 138, 67)) # color spaces min skin_ycrcb_max = np.array((255, 173, 133)) # color spaces max mask = cv2.inRange( blur, skin_ycrcb_min, skin_ycrcb_max ) # detecting the hand in the bounding box using skin detection imgres, contours, hierarchy = cv2.findContours( mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # track motion points cnt = ut.getMaxContour(contours, 4000) #store total motion points if cnt is not None: # is there something there? gesture, label = ut.getGestureImg( cnt, img1, mask, model ) # passing the trained model for prediction and fetching the result if ( label != None ): # is the model finding something in the box that matches a label? if (temp == 0): # if temp is 0 previouslabel = label # last label now label elif previouslabel == label: # still same gesture? previouslabel = label # track again temp += 1 # increse temp gesture count else: # otherwise temp = 0 # temp gesture back to 0 if (temp == 40): # has it been on screen for 40 temp counts? if (label == 'P'): # create sentence space if user does P label = " " # lable space text = text + label # add results to text if (label == 'Q'): # Wipe with Q(QUIT) words = re.split(" +", text) # split result words.pop() # push text off text = " ".join(words) # fresh text #text=previousText print(text) # print out your last gestures before quiting #cv2.imshow('PredictedGesture',gesture) # showing the best match or prediction cv2.putText( img, label, (50, 150), font, 8, (0, 125, 155), 2) # displaying the predicted letter on the main screen cv2.putText(img, text, (50, 450), font, 3, (0, 0, 255), 2) cv2.imshow('Frame', img) # show on screen #cv2.imshow('Mask',mask) key = cv2.waitKey(1) & 0xFF # wait for q to quit if key == ord("q"): # is Q? #cap.release() cv2.destroyAllWindows() # close OpenCV session camera.close() break # stop while return render_template("index.html")
previouslabel=None previousText=" " label = None while(cap.isOpened()): _,img=cap.read() cv2.rectangle(img,(900,100),(1300,500),(255,0,0),3) # bounding box which captures ASL sign to be detected by the system img1=img[100:500,900:1300] img_ycrcb = cv2.cvtColor(img1, cv2.COLOR_BGR2YCR_CB) blur = cv2.GaussianBlur(img_ycrcb,(11,11),0) skin_ycrcb_min = np.array((0, 138, 67)) skin_ycrcb_max = np.array((255, 173, 133)) mask = cv2.inRange(blur, skin_ycrcb_min, skin_ycrcb_max) # detecting the hand in the bounding box using skin detection contours,hierarchy = cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL, 2) cnt=ut.getMaxContour(contours,4000) # using contours to capture the skin filtered image of the hand if cnt!=None: gesture,label=ut.getGestureImg(cnt,img1,mask,model) # passing the trained model for prediction and fetching the result if(label!=None): if(temp==0): previouslabel=label if previouslabel==label : previouslabel=label temp+=1 else : temp=0 if(temp==40): if(label=='P'): label=" " text= text + label if(label=='Q'): words = re.split(" +",text)
thresh=thresh[0]-15 break while(cap.isOpened()): t=time.time() _,img=cap.read() gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret,th1 =cv2.threshold(gray,thresh,255,cv2.THRESH_TOZERO) cv2.imshow('threshold',th1) _,contours,hierarchy = cv2.findContours(th1.copy(),cv2.RETR_EXTERNAL, 2) cnts=ut.getContourBiggerThan(contours,minArea=3000,maxArea=40000) line=[] for cnt in cnts: x,y,w,h = cv2.boundingRect(cnt) _,resp=ut.getGestureImg(cnt,img,th1,model) M = cv2.moments(cnt) cx = int(M['m10']/M['m00']) cy = int(M['m01']/M['m00']) line.append((cx,cy)) cv2.circle(img,(cx,cy),5,[0,255,0],-1) cv2.putText(img,resp,(x,y), font,1,(255,255,255),2,cv2.LINE_AA) if len(line)==2: pt1=line[0] pt2=line[1] ang=int(ut.getSlope(pt1,pt2)) cv2.putText(img,'Angle-> '+str(ang),(400,50), font,1,(255,255,255),2,cv2.LINE_AA) if ang>0: cv2.putText(img,'RIGHT',((pt1[0]+pt2[0])/2,(pt1[1]+pt2[1])/2), font,3,(255,255,255),2,cv2.LINE_AA) else: cv2.putText(img,'LEFT',((pt1[0]+pt2[0])/2,(pt1[1]+pt2[1])/2), font,3,(255,255,255),2,cv2.LINE_AA)
model=st.trainSVM(9) #create and train SVM model each time coz bug in opencv 3.1.0 svm.load() https://github.com/Itseez/opencv/issues/4969 cam=int(raw_input("Enter Camera Index : ")) cap=cv2.VideoCapture(cam) font = cv2.FONT_HERSHEY_SIMPLEX while(cap.isOpened()): t=time.time() _,img=cap.read() gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret,th1 = cv2.threshold(gray.copy(),20,255,cv2.THRESH_BINARY) _,contours,hierarchy = cv2.findContours(th1.copy(),cv2.RETR_EXTERNAL, 2) cnt=ut.getMaxContour(contours,4000) if cnt!=None: gesture=ut.getGestureImg(cnt,img,th1,model) cv2.imshow('PredictedGesture',gesture) fps=int(1/(time.time()-t)) cv2.putText(img,"FPS: "+str(fps),(50,50), font,1,(255,255,255),2,cv2.LINE_AA) cv2.imshow('Frame',img) k = 0xFF & cv2.waitKey(10) if k == 27: break cap.release() cv2.destroyAllWindows()
print img_ycrcb blur = cv2.GaussianBlur(img_ycrcb, (11, 11), 0) print blur skin_ycrcb_min = np.array([0, 48, 80]) print skin_ycrcb_min skin_ycrcb_max = np.array([20, 255, 255]) mask = cv2.inRange( blur, skin_ycrcb_min, skin_ycrcb_max ) # detecting the hand in the bounding box using skin detection contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, 2) cnt = ut.getMaxContour( contours, 4000) # using contours to capture the skin filtered image of the hand print cnt if type(cnt) != type(None): gesture, label = ut.getGestureImg(cnt, img1, mask, model) #label=unichr(64+int(model.predict(st.hog_single(img)) )) # passing the trained model for prediction and fetching the result if (label != None): if (temp == 0): previouslabel = label if previouslabel == label: previouslabel = label temp += 1 else: temp = 0 if (temp == 40): if (label == 'P'): label = " " text = text + label if (label == 'Q'):
temp = 0 previouslabel = None previousText = " " label = None text = "" while (cap.isOpened()): move = '' t = time.time() _, img = cap.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, th1 = cv2.threshold(gray.copy(), 150, 255, cv2.THRESH_TOZERO) cv2.imshow('thresh', th1) _, contours, hierarchy = cv2.findContours(th1.copy(), cv2.RETR_EXTERNAL, 2) cnt = ut.getMaxContour(contours, 4000) if cnt.any() != None: gesture, label = ut.getGestureImg(cnt, img, th1, model) if label != None: if temp == 0: previouslabel = label if previouslabel == label: previouslabel = label temp += 1 else: temp = 0 if (temp == 40): if (label == 'P'): label = " " text += label if (label == 'Q'): words = re.split(" +", text)
def start_webcam(): cap = cv2.VideoCapture(0) cap.set(4, 1024) font = cv2.FONT_HERSHEY_SIMPLEX text = " " temp = 0 previouslabel = None previousText = " " label = None flag = True words = "" def rescale_frame(frame, percent): scale_percent = 100 width = int(frame.shape[1] * scale_percent / 100) height = int(frame.shape[0] * percent / 100) dim = (width, height) return cv2.resize(frame, dim, interpolation=cv2.INTER_AREA) while (flag): _, img = cap.read() img = rescale_frame(img, percent=94) cv2.rectangle( img, (620, 100), (1000, 500), (255, 0, 0), 3 ) # bounding box which captures ASL sign to be detected by the system img1 = img[100:500, 620:1000] img_ycrcb = cv2.cvtColor(img1, cv2.COLOR_BGR2YCR_CB) blur = cv2.GaussianBlur(img_ycrcb, (11, 11), 0) skin_ycrcb_min = np.array((0, 138, 67)) skin_ycrcb_max = np.array((255, 173, 133)) mask = cv2.inRange( blur, skin_ycrcb_min, skin_ycrcb_max ) # detecting the hand in the bounding box using skin detection _, contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, 2) cnt = ut.getMaxContour( contours, 4000 ) # using contours to capture the skin filtered image of the hand # cnt!= None if cnt is not None: gesture, label = ut.getGestureImg( cnt, img1, mask, model ) # passing the trained model for prediction and fetching the result if (label != None): if (temp == 0): previouslabel = label if previouslabel == label: previouslabel = label temp += 1 else: temp = 0 if (temp == 30): if (label == 'Q'): label = " " text = text + label if (label == 'W'): words = list(text) print(words) words.pop() words.pop() text = "".join(words) # text=previousText print(text) cv2.imshow('PredictedGesture', gesture) # showing the best match or prediction cv2.putText( img, label, (50, 150), font, 8, (0, 125, 155), 2) # displaying the predicted letter on the main screen cv2.putText(img, text, (50, 450), font, 3, (0, 0, 255), 2) cv2.imshow('Frame', img) cv2.imshow('Mask', mask) k = 0xFF & cv2.waitKey(10) if k == 27: flag = False cv2.imshow('Frame', img) cap.release() words = list(text) print(words) words = "".join(words) text_inil = "[Kullanıcı 1] :" deneme = text_inil + words + "\n " text61.insert(INSERT, deneme.upper()) print(deneme) print('aaaaaaaaaa')