Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open innovation PR #60

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# import cv2
# (width, height) = (130, 100)
# cap = cv2.VideoCapture(0)
# while (cap.isOpened()):
# ret, img = cap.read()
# img=cv2.flip(img, 1)
# cv2.rectangle(img, (20, 20), (250, 250), (255, 0, 0), 3)
# cv2.imshow("RGB Output", img)
# img1 = img[20:250,20:250]
# imCopy = img1.copy()
# gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
# blur = cv2.GaussianBlur(gray, (5, 5), 0)
# ret, thresh1 = cv2.threshold(blur, 10, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# hand_resize = cv2.resize(thresh1, (width, height))
# cv2.imshow("Threshold", thresh1)
# contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# cv2.drawContours(imCopy, contours, -1, (0, 255, 0))
# cv2.imshow('Draw Contours', imCopy)

# k = 0xFF & cv2.waitKey(10)
# if k == 27:
# break

# cap.release()
# cv2.destroyAllWindows()

# STATIC IMAGES INPUT

import cv2
img = cv2.imread('img.jpg')
cv2.imshow("Original Image", img)
img1 = cv2.resize(img, (300, 300))
cv2.imshow("Resized image", img1)
# ret, img = cap.read()
img2=cv2.flip(img1, 1)
cv2.imshow("Flipped image", img2)
cv2.rectangle(img2, (20, 20), (250, 250), (255, 0, 0), 3)
cv2.imshow("RGB Output", img2)
img3 = img2[20:250,20:250]
imCopy = img3.copy()
gray = cv2.cvtColor(img3, cv2.COLOR_BGR2GRAY)
cv2.imshow("grayscale",gray)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
cv2.imshow("GaussianBlur",blur)
ret, thresh1 = cv2.threshold(blur, 10, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
hand_resize = cv2.resize(thresh1, (500, 400))
cv2.imshow("Threshold", thresh1)
contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(imCopy, contours, -1, (0, 0, 0))
cv2.imshow('Draw Contours', imCopy)
# cv2.imshow("Image", img2)
interrupt = cv2.waitKey(0) & 0xFF
if interrupt == 27:
cv2.destroyAllWindows()
79 changes: 79 additions & 0 deletions collect-data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import cv2
import os

if not os.path.exists("data"):
os.makedirs("data")
os.makedirs("data/train")
os.makedirs("data/train/0")
os.makedirs("data/train/1")
os.makedirs("data/train/2")
os.makedirs("data/train/3")
os.makedirs("data/train/4")
os.makedirs("data/train/5")


mode = 'train'
directory = 'data/'+mode+'/'

# url = '<YOUR IP ADDRESS>/video'
# cap=cv2.VideoCapture(url)

cap=cv2.VideoCapture(0)

while True:
_, frame = cap.read()
frame = cv2.flip(frame, 1)

cv2.putText(frame, "Sayak-Rana", (160, 240), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,0,0), 3)

count = {'zero': len(os.listdir(directory+"/0")),
'one': len(os.listdir(directory+"/1")),
'two': len(os.listdir(directory+"/2")),
'three': len(os.listdir(directory+"/3")),
'four': len(os.listdir(directory+"/4")),
'five': len(os.listdir(directory+"/5"))}

cv2.putText(frame, "MODE : "+mode, (10, 50), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,0,0), 1)
cv2.putText(frame, "IMAGE COUNT", (10, 90), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,0,0), 1)
cv2.putText(frame, "ZERO : "+str(count['zero']), (10, 115), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,0,0), 1)
cv2.putText(frame, "ONE : "+str(count['one']), (10, 140), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,0,0), 1)
cv2.putText(frame, "TWO : "+str(count['two']), (10, 165), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,0,0), 1)
cv2.putText(frame, "THREE : "+str(count['three']), (10, 190), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,0,0), 1)
cv2.putText(frame, "FOUR : "+str(count['four']), (10, 215), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,0,0), 1)
cv2.putText(frame, "FIVE : "+str(count['five']), (10, 240), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,0,0), 1)


x1 = int(0.5*frame.shape[1])
y1 = 10
x2 = frame.shape[1]-10
y2 = int(0.5*frame.shape[1])
cv2.rectangle(frame, (x1-1, y1-1), (x2+1, y2+1), (255,0,0) ,3)
roi = frame[y1:y2, x1:x2]
roi = cv2.resize(roi, (200, 200))
cv2.putText(frame, "R.O.I", (440, 350), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,225,0), 3)
cv2.imshow("Frame", frame)

roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
_, roi = cv2.threshold(roi, 120, 255, cv2.THRESH_OTSU)
cv2.imshow("ROI", roi)



interrupt = cv2.waitKey(10)
if interrupt & 0xFF == 27:
break
if interrupt & 0xFF == ord('0'):
cv2.imwrite(directory+'0/'+str(count['zero'])+'.jpg', roi)
if interrupt & 0xFF == ord('1'):
cv2.imwrite(directory+'1/'+str(count['one'])+'.jpg', roi)
if interrupt & 0xFF == ord('2'):
cv2.imwrite(directory+'2/'+str(count['two'])+'.jpg', roi)
if interrupt & 0xFF == ord('3'):
cv2.imwrite(directory+'3/'+str(count['three'])+'.jpg', roi)
if interrupt & 0xFF == ord('4'):
cv2.imwrite(directory+'4/'+str(count['four'])+'.jpg', roi)
if interrupt & 0xFF == ord('5'):
cv2.imwrite(directory+'5/'+str(count['five'])+'.jpg', roi)

cap.release()
cv2.destroyAllWindows()
69 changes: 69 additions & 0 deletions defects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import cv2
import numpy as np
import math

cap=cv2.VideoCapture(0)

while(cap.isOpened()):
ret, img = cap.read()
img=cv2.flip(img, 1)
cv2.rectangle(img,(20,20),(250,250),(255,0,0),3)
crop_img = img[20:250, 20:250]
grey = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
value = (35, 35)
blurred = cv2.GaussianBlur(grey, value, 0)
_, thresh1 = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
contours, hierarchy = cv2.findContours(thresh1.copy(),cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnt = max(contours, key = lambda x: cv2.contourArea(x))
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(crop_img,(x,y),(x+w,y+h),(0,0,255),0)

hull = cv2.convexHull(cnt)
drawing = np.zeros(crop_img.shape,np.uint8)

cv2.drawContours(drawing,[cnt],0,(0,255,0),0)
cv2.drawContours(drawing,[hull],0,(0,0,255),0)

hull = cv2.convexHull(cnt,returnPoints = False)
defects = cv2.convexityDefects(cnt,hull)

count_defects = 0
cv2.drawContours(thresh1, contours, -1, (0,255,0), 3)

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])
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)) * 57

if angle <= 90:
count_defects += 1
cv2.circle(crop_img,far,1,[0,0,255],-1)

cv2.line(crop_img,start,end,[0,255,0],2)

if count_defects == 1:
cv2.putText(img,"Number : 2", (50,450), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 1)
elif count_defects == 2:
cv2.putText(img, "Number : 3", (50,450), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 1)
elif count_defects == 3:
cv2.putText(img,"Number : 4", (50,450), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 1)
elif count_defects == 4:
cv2.putText(img,"Number : 5", (50,450), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 1)
elif count_defects == 5:
cv2.putText(img,"Number : 6", (50,450), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 1)
else:
cv2.putText(img,"Number : 1", (50,450), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 1)

cv2.imshow('Gesture', img)
cv2.imshow('Contours', drawing)
cv2.imshow('Defects', crop_img)
cv2.imshow('Binary Image', thresh1)

k = cv2.waitKey(10)
if k == 27:
break
35 changes: 35 additions & 0 deletions face_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# FACE DETECTION FOR VIDEO FROM WEBCAM
# if you are importing any photo comment out this part upto cap.release()
import cv2
# Doing by importing a cascade
cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = cascade.detectMultiScale(grayscale, 1.1, 3)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('img', frame)
k = cv2.waitKey(30) & 0xff
if k==27:
break
cap.release()

#------------------------------------------------------------------------------#

# FACE DETECTION FROM PHOTOS
# if you want to get output from videos via webcam comment out upto cv2.waitkey()
# import cv2
# # Doing by importinfg the cascade
# cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# img2 = cv2.imread('img1.jpg') #since i am taking input from img1.jpg
# img = cv2.resize(img2, (1300, 700))
# grayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# faces = cascade.detectMultiScale(grayscale, 1.1, 3)
# for (x, y, w, h) in faces:
# cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
# cv2.imshow('img', img)
# cv2.waitKey()

#------------------------------------------------------------------------------#
Loading