Skip to content

Commit

Permalink
Update segment.py
Browse files Browse the repository at this point in the history
Gogul09 authored Apr 7, 2017
1 parent 59047b5 commit 4935392
Showing 1 changed file with 80 additions and 74 deletions.
154 changes: 80 additions & 74 deletions segment.py
Original file line number Diff line number Diff line change
@@ -10,110 +10,116 @@
# Function - To find the running average over the background
#-------------------------------------------------------------------------------
def run_avg(image, accumWeight):
global bg
# initialize the background
if bg is None:
bg = image.copy().astype("float")
return
global bg
# initialize the background
if bg is None:
bg = image.copy().astype("float")
return

# compute weighted average, accumulate it and update the background
cv2.accumulateWeighted(image, bg, accumWeight)
# compute weighted average, accumulate it and update the background
cv2.accumulateWeighted(image, bg, accumWeight)

#-------------------------------------------------------------------------------
# Function - To segment the region of hand in the image
#-------------------------------------------------------------------------------
def segment(image, threshold=25):
global bg
# find the absolute difference between background and current frame
diff = cv2.absdiff(bg.astype("uint8"), image)
global bg
# find the absolute difference between background and current frame
diff = cv2.absdiff(bg.astype("uint8"), image)

# threshold the diff image so that we get the foreground
thresholded = cv2.threshold(diff,
threshold,
255,
cv2.THRESH_BINARY)[1]

# get the contours in the thresholded image
(_, cnts, _) = cv2.findContours(thresholded.copy(),
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)

# return None, if no contours detected
if len(cnts) == 0:
return
else:
# based on contour area, get the maximum contour which is the hand
segmented = max(cnts, key=cv2.contourArea)
return (thresholded, segmented)

# threshold the diff image so that we get the foreground
thresholded = cv2.threshold(diff, threshold, 255, cv2.THRESH_BINARY)[1]

# get the contours in the thresholded image
(_, cnts, _) = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# return None, if no contours detected
if len(cnts) == 0:
return
else:
# based on contour area, get the maximum contour which is the hand
segmented = max(cnts, key=cv2.contourArea)
return (thresholded, segmented)

#-------------------------------------------------------------------------------
# Main function
#-------------------------------------------------------------------------------
if __name__ == "__main__":
# initialize accumulated weight
accumWeight = 0.5
# initialize accumulated weight
accumWeight = 0.5

# get the reference to the webcam
camera = cv2.VideoCapture(0)
# get the reference to the webcam
camera = cv2.VideoCapture(0)

# region of interest (ROI) coordinates
top, right, bottom, left = 10, 350, 225, 590
# region of interest (ROI) coordinates
top, right, bottom, left = 10, 350, 225, 590

# initialize num of frames
num_frames = 0
# initialize num of frames
num_frames = 0

# keep looping, until interrupted
while(True):
# get the current frame
(grabbed, frame) = camera.read()
# keep looping, until interrupted
while(True):
# get the current frame
(grabbed, frame) = camera.read()

# resize the frame
frame = imutils.resize(frame, width=700)
# resize the frame
frame = imutils.resize(frame, width=700)

# flip the frame so that it is not the mirror view
frame = cv2.flip(frame, 1)
# flip the frame so that it is not the mirror view
frame = cv2.flip(frame, 1)

# clone the frame
clone = frame.copy()
# clone the frame
clone = frame.copy()

# get the height and width of the frame
(height, width) = frame.shape[:2]
# get the height and width of the frame
(height, width) = frame.shape[:2]

# get the ROI
roi = frame[top:bottom, right:left]
# get the ROI
roi = frame[top:bottom, right:left]

# convert the roi to grayscale and blur it
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)
# convert the roi to grayscale and blur it
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)

# to get the background, keep looking till a threshold is reached
# so that our weighted average model gets calibrated
if num_frames < 30:
run_avg(gray, accumWeight)
else:
# segment the hand region
hand = segment(gray)
# to get the background, keep looking till a threshold is reached
# so that our weighted average model gets calibrated
if num_frames < 30:
run_avg(gray, accumWeight)
else:
# segment the hand region
hand = segment(gray)

# check whether hand region is segmented
if hand is not None:
# if yes, unpack the thresholded image and
# segmented region
(thresholded, segmented) = hand
# check whether hand region is segmented
if hand is not None:
# if yes, unpack the thresholded image and
# segmented region
(thresholded, segmented) = hand

# draw the segmented region and display the frame
cv2.drawContours(clone, [segmented + (right, top)], -1, (0, 255, 0))
cv2.imshow("Thesholded", thresholded)
# draw the segmented region and display the frame
cv2.drawContours(clone, [segmented + (right, top)], -1, (0, 0, 255))
cv2.imshow("Thesholded", thresholded)

# draw the segmented hand
cv2.rectangle(clone, (left, top), (right, bottom), (0,255,0), 2)
# draw the segmented hand
cv2.rectangle(clone, (left, top), (right, bottom), (0,255,0), 2)

# increment the number of frames
num_frames += 1
# increment the number of frames
num_frames += 1

# display the frame with segmented hand
cv2.imshow("Video Feed", clone)
# display the frame with segmented hand
cv2.imshow("Video Feed", clone)

# observe the keypress by the user
keypress = cv2.waitKey(1) & 0xFF
# observe the keypress by the user
keypress = cv2.waitKey(1) & 0xFF

# if the user pressed "q", then stop looping
if keypress == ord("q"):
break
# if the user pressed "q", then stop looping
if keypress == ord("q"):
break

# free up memory
camera.release()

0 comments on commit 4935392

Please sign in to comment.