[SOLVED]Can't load picture after finger detection open cv python. [closed]
.
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 24 20:27:59 2020
@author: Shrouti
"""
import numpy as np
import cv2
import math
import subprocess
import time
def calculateFingers(res,drawing):
hull = cv2.convexHull(res, returnPoints=False)
if len(hull) > 3:
defects = cv2.convexityDefects(res, hull)
if type(defects) != type(None): # avoid crashing. (BUG not found)
cnt = 0
for i in range(defects.shape[0]): # calculate the angle
s, e, f, d = defects[i][0]
start = tuple(res[s][0])
end = tuple(res[e][0])
far = tuple(res[f][0])
a = math.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2)
b = math.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2)
c = math.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2)
angle = math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) # cosine theorem
if angle <= math.pi / 2: # angle less than 90 degree, treat as fingers
cnt += 1
cv2.circle(drawing, far, 8, [211, 84, 0], -1)
return True, cnt
return False, 0
img = cv2.imread('C:\Users\Shrouti\Pictures\hand_gesture3.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# blur the image
blur = cv2.blur(gray, (3, 3))
#blur = cv2.GaussianBlur(img, (blur, blur), 0)
ret, thresh = cv2.threshold(blur, 60, 255, cv2.THRESH_BINARY)
cnts, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(cnts) > 0 :
c = max(cnts, key=cv2.contourArea)
hull = cv2.convexHull(c)
drawing = np.zeros([384,320,3], np.uint8)
cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 3)
cv2.drawContours(drawing, [c], 0, (0, 0, 255), 3)
cv2.imshow('image', drawing)
isFinishCal,cnt = calculateFingers(c,drawing)
# if((cnt-prev) != 0) :
if(cnt == 4) :
subprocess.call(['echo', 'Gesture 5 detected'], shell=True)
prev = cnt
# if((cnt-prev) != 0) :
if(cnt == 3) :
subprocess.call(['echo', 'Gesture 4 detected'], shell=False)
prev = cnt
# if((cnt-prev) != 0) :
if(cnt == 2) :
subprocess.call(['echo', 'Gesture 3 detected'], shell=True)
prev = cnt
# if((cnt-prev) != 0) :
if(cnt == 1) :
subprocess.call(['echo', 'Gesture 2 detected'], shell=True)
prev = cnt
#cv2.putText(img,'cnt',(10,250), cv2.FONT_HERSHEY_SIMPLEX, 4,(255,255,255),2,cv2.LINE_AA)
print cnt+1
In the above code fingure count is being shown, but no image is being loaded. Please help. I am not getting any output of "cv2.drawcontours()" function also.
you need to call
cv2.waitKey()
aftercv2.imshow()
else it won't draw anythingwhat do you mean ? it would crash without an image loaded.
also, please put your
cv2.putText
before imshow, else you don't see it (or the wrong txt)thank you ...it works now :)