Commit 8e9f56b1 authored by K.A Rasanjalee's avatar K.A Rasanjalee

Upload New File

parent b0027b9f
from flask import Flask, render_template, Response, request
import cv2
import datetime, time
import os, sys
import numpy as np
from cv2 import imread
from cvzone.HandTrackingModule import HandDetector
from time import sleep
global capture,rec_frame, grey, switch, neg, face, rec, out
capture=0
grey=0
neg=0
face=0
switch=1
rec=0
#make shots directory to save pics
# try:
# os.mkdir('./shots')
# except OSError as error:
# pass
#Load pretrained face detection model
# net = cv2.dnn.readNetFromCaffe('./saved_model/deploy.prototxt.txt', './saved_model/res10_300x300_ssd_iter_140000.caffemodel')
#instatiate flask app
app = Flask(__name__, template_folder='./templates')
camera = cv2.VideoCapture(0)
# def record(out):
# global rec_frame
# while(rec):
# time.sleep(0.05)
# out.write(rec_frame)
def detect_face(frame):
global net
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0,
(300, 300), (104.0, 177.0, 123.0))
net.setInput(blob)
detections = net.forward()
confidence = detections[0, 0, 0, 2]
if confidence < 0.5:
return frame
box = detections[0, 0, 0, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
try:
frame=frame[startY:endY, startX:endX]
(h, w) = frame.shape[:2]
r = 480 / float(h)
dim = ( int(w * r), 480)
frame=cv2.resize(frame,dim)
except Exception as e:
pass
return frame
def gen_frames(): # generate frame by frame from camera
# global out, capture,rec_frame
while True:
success, frame = camera.read()
if success:
# if(face):
# frame= detect_face(frame)
# if(grey):
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# if(neg):
# frame=cv2.bitwise_not(frame)
# if(capture):
# capture=0
# now = datetime.datetime.now()
# p = os.path.sep.join(['shots', "shot_{}.png".format(str(now).replace(":",''))])
# cv2.imwrite(p, frame)
# if(rec):
# rec_frame=frame
# frame= cv2.putText(cv2.flip(frame,1),"Recording...", (0,25), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255),4)
# frame=cv2.flip(frame,1)
# try:
# ret, buffer = cv2.imencode('.jpg', cv2.flip(frame,1))
# frame = buffer.tobytes()
# yield (b'--frame\r\n'
# b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
# except Exception as e:
# pass
detector = HandDetector(detectionCon = 0.8)
img1 =cv2.imread("Alphabet\q.png")
ox,oy =20 ,40 #ox->originx
img2 =cv2.imread("Alphabet\w.png")
ox2,oy2 =80 ,40 #ox->originx
img3 =cv2.imread("Alphabet\e.png")
ox3,oy3 =140 ,40 #ox->originx
img4 =cv2.imread("Alphabet\\r.png")
ox4,oy4 =200 ,40 #ox->originx
img5 =cv2.imread("Alphabet\\t.png")
ox5,oy5 =260 ,40 #ox->originx
img6 =cv2.imread("Alphabet\y.png")
ox6,oy6 =320,40 #ox->originx
img7 =cv2.imread("Alphabet\\u.png")
ox7,oy7 =380 ,40 #ox->originx
img8 =cv2.imread("Alphabet\i.png")
ox8,oy8 =440 ,40 #ox->originx
img9 =cv2.imread("Alphabet\o.png")
ox9,oy9 =500 ,40 #ox->originx
img10 =cv2.imread("Alphabet\p.png")
ox10,oy10 =560 ,40 #ox->originx
img11 =cv2.imread("Alphabet\\a.png")
ox11,oy11 =20 ,110 #ox->originx
img12 =cv2.imread("Alphabet\s.png")
ox12,oy12 =80 ,110 #ox->originx
img13 =cv2.imread("Alphabet\d.png")
ox13,oy13 =140 ,110 #ox->originx
img14 =cv2.imread("Alphabet\\f.png")
ox14,oy14 =200 ,110 #ox->originx
img15 =cv2.imread("Alphabet\g.png")
ox15,oy15 =260 ,110 #ox->originx
img16 =cv2.imread("Alphabet\h.png")
ox16,oy16 =320,110 #ox->originx
img17 =cv2.imread("Alphabet\j.png")
ox17,oy17 =380 ,110 #ox->originx
img18 =cv2.imread("Alphabet\k.png")
ox18,oy18 =440 ,110 #ox->originx
img19 =cv2.imread("Alphabet\l.png")
ox19,oy19 =500 ,110 #ox->originx
img20 =cv2.imread("Alphabet\z.png")
ox20,oy20 =20 ,180 #ox->originx
img21 =cv2.imread("Alphabet\\x.png")
ox21,oy21 =80 ,180 #ox->originx
img22 =cv2.imread("Alphabet\c.png")
ox22,oy22 =140 ,180 #ox->originx
img23 =cv2.imread("Alphabet\\v.png")
ox23,oy23 =200 ,180 #ox->originx
img24 =cv2.imread("Alphabet\\b.png")
ox24,oy24 =260 ,180 #ox->originx
img25 =cv2.imread("Alphabet\\n.png")
ox25,oy25 =320,180 #ox->originx
img26 =cv2.imread("Alphabet\m.png")
ox26,oy26 =380 ,180 #ox->originx
img27 =cv2.imread("1-10\\1.png")
ox27,oy27 =20 ,250 #ox->originx
img28 =cv2.imread("1-10\\2.png")
ox28,oy28 =80 ,250 #ox->originx
img29 =cv2.imread("1-10\\3.png")
ox29,oy29 =140 ,250 #ox->originx
img30 =cv2.imread("1-10\\4.png")
ox30,oy30 =200 ,250 #ox->originx
img31 =cv2.imread("1-10\\5.png")
ox31,oy31 =260 ,250 #ox->originx
img32 =cv2.imread("1-10\\6.png")
ox32,oy32 =320,250 #ox->originx
img33 =cv2.imread("1-10\\7.png")
ox33,oy33 =380,250 #ox->originx
img34 =cv2.imread("1-10\8.png")
ox34,oy34 =440 ,250 #ox->originx
img35 =cv2.imread("1-10\9.png")
ox35,oy35 =500,250 #ox->originx
img36 =cv2.imread("1-10\\10.png")
ox36,oy36 =560 ,250 #ox->originx
keys =[["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"], #LIST NO 1
["A", "S", "D", "F", "G", "H", "J", "K", "L", ";"], #LIST NO 2
["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"],#LIST NO 3
["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]]
final_text = ""
def drawALL(img,buttonList):
for button in buttonList:
x,y =button.pos
w,h =button.size
cv2.rectangle(img, button.pos,(x+w,y+h),(100,0,100),cv2.FILLED)
# img, (initial loction),(size)
cv2.putText(img,button.text, (x+10, y+35)
,cv2.FONT_HERSHEY_PLAIN,2,(255,255,255),2)
return img
class Button():
def __init__(self,pos,text,size=[49,54]):
self.pos = pos
self.size = size
self.text = text
buttonlist =[]
for i in range(len(keys)) :
for j, key in enumerate(keys[i]) :
buttonlist.append(Button([60*j+20,70*i+40],key ))
while True:
success ,img=camera.read()
img =cv2.flip(img ,1)
hands ,img = detector.findHands(img,flipType=False) #with draw
img = drawALL(img,buttonlist)
cv2.rectangle(img, (30,350), (100,310),(100,0,100), cv2.FILLED)
cv2.putText(img, 'clr', (40, 340), cv2.FONT_HERSHEY_PLAIN, 2,(255,255,255), 2)
cv2.rectangle(img, (150,350), (450,310),(100,0,100), cv2.FILLED)
cv2.putText(img, 'Space', (250, 340), cv2.FONT_HERSHEY_PLAIN, 2,(255,255,255), 2)
cv2.rectangle(img, (600,350), (500,310),(100,0,100), cv2.FILLED)
cv2.putText(img, '<--', (520, 340), cv2.FONT_HERSHEY_PLAIN, 2,(255,255,255), 2)
h,w,_ =img1.shape
img[oy:oy+h,ox:ox+w] = img1
h2,w2,_ =img2.shape
img[oy2:oy2+h2,ox2:ox2+w2] = img2
h3,w3,_ =img3.shape
img[oy3:oy3+h3,ox3:ox3+w3] = img3
h4,w4,_ =img4.shape
img[oy4:oy4+h4,ox4:ox4+w4] = img4
h5,w5,_ =img5.shape
img[oy5:oy5+h5,ox5:ox5+w5] = img5
h6,w6,_ =img6.shape
img[oy6:oy6+h6,ox6:ox6+w6] = img6
h7,w7,_ =img7.shape
img[oy7:oy7+h7,ox7:ox7+w7] = img7
h8,w8,_ =img8.shape
img[oy8:oy8+h8,ox8:ox8+w8] = img8
h9,w9,_ =img9.shape
img[oy9:oy9+h9,ox9:ox9+w9] = img9
h10,w10,_ =img10.shape
img[oy10:oy10+h10,ox10:ox10+w10] = img10
h11,w11,_ =img11.shape
img[oy11:oy11+h11,ox11:ox11+w11] = img11
h12,w12,_ =img12.shape
img[oy12:oy12+h12,ox12:ox12+w12] = img12
h13,w13,_ =img13.shape
img[oy13:oy13+h13,ox13:ox13+w13] = img13
h14,w14,_ =img14.shape
img[oy14:oy14+h14,ox14:ox14+w14] = img14
h15,w15,_ =img15.shape
img[oy15:oy15+h15,ox15:ox15+w15] = img15
h16,w16,_ =img16.shape
img[oy16:oy16+h16,ox16:ox16+w16] = img16
h17,w17,_ =img17.shape
img[oy17:oy17+h17,ox17:ox17+w17] = img17
h18,w18,_ =img18.shape
img[oy18:oy18+h18,ox18:ox18+w18] = img18
h19,w19,_ =img19.shape
img[oy19:oy19+h19,ox19:ox19+w19] = img19
h20,w20,_ =img20.shape
img[oy20:oy20+h20,ox20:ox20+w20] = img20
h21,w21,_ =img21.shape
img[oy21:oy21+h21,ox21:ox21+w21] = img21
h22,w22,_ =img22.shape
img[oy22:oy22+h22,ox22:ox22+w22] = img22
h23,w23,_ =img23.shape
img[oy24:oy24+h23,ox23:ox23+w23] = img23
h24,w24,_ =img24.shape
img[oy24:oy24+h24,ox24:ox24+w24] = img24
h25,w25,_ =img25.shape
img[oy25:oy25+h25,ox25:ox25+w25] = img25
h26,w26,_ =img26.shape
img[oy26:oy26+h26,ox26:ox26+w26] = img26
#numbers
h27,w27,_ =img27.shape
img[oy27:oy27+h27,ox27:ox27+w27] = img27
h28,w28,_ =img28.shape
img[oy28:oy28+h28,ox28:ox28+w28] = img28
h29,w29,_ =img29.shape
img[oy29:oy29+h29,ox29:ox29+w29] = img29
h30,w30,_ =img30.shape
img[oy30:oy30+h30,ox30:ox30+w30] = img30
h31,w31,_ =img31.shape
img[oy31:oy31+h31,ox31:ox31+w31] = img31
h32,w32,_ =img32.shape
img[oy32:oy32+h32,ox32:ox32+w32] = img32
h33,w33,_ =img33.shape
img[oy33:oy33+h33,ox33:ox33+w33] = img33
h34,w34,_ =img34.shape
img[oy34:oy34+h34,ox34:ox34+w34] = img34
h35,w35,_ =img35.shape
img[oy35:oy35+h35,ox35:ox35+w35] = img35
h36,w36,_ =img36.shape
img[oy36:oy36+h36,ox36:ox36+w36] = img36
if hands:
#hand 1
hand1 = hands[0] #detecting only one hand
lmList1 = hand1["lmList"] #21 parameters,21 landmarks points
#lmList2 = hand1["lmList"]
bbox1 =hand1["bbox"] #bounding box info x,y,w,h
centerPoint1 =hand1["center"] #gives center of the hand cx and cy
handType1 =hand1["type"] #hand type left or right
b1,b2,b3,b4 =bbox1
if b3>80 and b4>100:
if lmList1:
x1,y1 = 30,310
w1,h1 = 100,350
#checking the x and y position
if 30 < lmList1[8][0]<100 and 310<lmList1[8][1]< 350: #index finger tip
cv2.rectangle(img, (30,350), (100,310),(195,0,195), cv2.FILLED)
cv2.putText(img, 'clr', (40, 340), cv2.FONT_HERSHEY_PLAIN, 2,(255,255,255), 2)
length1,info1 = detector.findDistance(lmList1[4],lmList1[8])
#print(lmList1[8][1])
#print(length1)
if length1<30:
cv2.rectangle(img, (30,350), (100,310),(0,255,0), cv2.FILLED)
cv2.putText(img, 'clr', (40, 340), cv2.FONT_HERSHEY_PLAIN, 2,(255,255,255), 2)
final_text=''
sleep(0.15) #how many times we clicked
if 150 < lmList1[8][0]<450 and 310<lmList1[8][1]<350: #index finger tip
cv2.rectangle(img, (150,350), (450,310),(195,0,195), cv2.FILLED)
cv2.putText(img, 'Space', (250, 340), cv2.FONT_HERSHEY_PLAIN, 2,(255,255,255), 2)
length2,info2 = detector.findDistance(lmList1[4],lmList1[8])
#print(lmList1[8][1])
if length2<30:
cv2.rectangle(img, (150,350), (450,310),(0,255,0), cv2.FILLED)
cv2.putText(img, 'Space', (250, 340), cv2.FONT_HERSHEY_PLAIN, 2,(255,255,255), 2)
final_text +=' '
sleep(0.15) #how many times we clicked
if 495 < lmList1[8][0]<600 and 310<lmList1[8][1]< 350: #index finger tip
cv2.rectangle(img, (600,350), (500,310),(195,0,195), cv2.FILLED)
cv2.putText(img, '<--', (520, 340), cv2.FONT_HERSHEY_PLAIN, 2,(255,255,255), 2)
length3,info3 = detector.findDistance(lmList1[4],lmList1[8])
# print(lmList1[8][0])
if length3<30:
cv2.rectangle(img, (600,350), (500,310),(0,255,0), cv2.FILLED)
cv2.putText(img, '<--', (520, 340), cv2.FONT_HERSHEY_PLAIN, 2,(255,255,255), 2)
final_text = final_text[:-1]
sleep(0.15) #how many times we clicked
for button in buttonlist:
x,y = button.pos
w,h = button.size
if x< lmList1[8][0]<x+w and y<lmList1[8][1]< y+h: #index finger tip
cv2.rectangle(img, button.pos,(x+w,y+h),(195,0,195),cv2.FILLED)
# img, (initial loction),(size)
cv2.putText(img,button.text, (x+10, y+35),cv2.FONT_HERSHEY_PLAIN,2,(255,255,255),2)
length,info = detector.findDistance(lmList1[4],lmList1[8])
#l , _, _ =detector.findDistance(8,12,img)
#print(length)
#when clicked
if length<30:
cv2.rectangle(img, button.pos,(x+w,y+h),(0,255,0),cv2.FILLED)
# img, (initial loction),(size)
cv2.putText(img,button.text, (x+10, y+35),cv2.FONT_HERSHEY_PLAIN,2,(255,255,255),2)
final_text+=button.text
sleep(0.15) #how many times we clicked
#space button
# space_text=" "
# cv2.rectangle(img, (80,350),(300,310),(195,0,195),cv2.FILLED)
# # img, (initial loction),(size)
# cv2.putText(img,space_text, (60, 390),cv2.FONT_HERSHEY_PLAIN,2,(255,255,255),2)
#final text
cv2.rectangle(img, (50,370),(580,450),(255,255,255),cv2.FILLED)
# img, (initial loction),(size)
cv2.putText(img,final_text, (60, 430),cv2.FONT_HERSHEY_PLAIN,3,(0,0,0),3)
# hands = detector.findHands(img,draw=False) #no draw
# print(len(hands))
# lmList ,bboxInfo = detector.findHands(img)
# cv2.namedWindow("image", cv2.WINDOW_NORMAL)
# set your desired size
cv2.resizeWindow('image', 1266 , 698 )
cv2.imshow("image",img)
#cv2.geometry('1485x500')
cv2.waitKey(1)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/requests',methods=['POST','GET'])
def tasks():
global switch,camera
if request.method == 'POST':
# if request.form.get('click') == 'Capture':
# global capture
# capture=1
# elif request.form.get('grey') == 'Grey':
# global grey
# grey=not grey
# elif request.form.get('neg') == 'Negative':
# global neg
# neg=not neg
# elif request.form.get('face') == 'Face Only':
# global face
# face=not face
# if(face):
# time.sleep(4)
if request.form.get('stop') == 'Stop/Start':
if(switch==1):
switch=0
camera.release()
cv2.destroyAllWindows()
else:
camera = cv2.VideoCapture(0)
switch=1
# elif request.form.get('rec') == 'Start/Stop Recording':
# global rec, out
# rec= not rec
# if(rec):
# now=datetime.datetime.now()
# fourcc = cv2.VideoWriter_fourcc(*'XVID')
# out = cv2.VideoWriter('vid_{}.avi'.format(str(now).replace(":",'')), fourcc, 20.0, (640, 480))
# #Start new thread for recording the video
# thread = Thread(target = record, args=[out,])
# thread.start()
# elif(rec==False):
# out.release()
elif request.method=='GET':
return render_template('index.html')
return render_template('index.html')
if __name__ == '__main__':
app.run()
camera.release()
cv2.destroyAllWindows()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment