<jupyter_start><jupyter_text>Welcome to facemap  Login to the facemap node app and start the session<jupyter_code>import sys
# Here are all the installs and imports you will need for facemap to run.
    
!{sys.executable} -m pip install requests
# !{sys.executable} -m pip install socketIO-client
!{sys.executable} -m pip install opencv-python pip install pillow
!{sys.executable} -m pip install pillow
!{sys.executable} -m pip install "python-socketio[client]"

import requests
import socketio
import cv2
import os
import numpy as np
from PIL import Image
import pickle
import random
# just camera test

import numpy as np
import cv2
from PIL import Image

cap = cv2.VideoCapture("C:/Users/17211/Desktop/chakri_sample.mp4")
face_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_default.xml')
cap.set(3,640) # set Width
cap.set(4,480) # set Height


try:
    while(True):
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        faces = face_cascade.detectMultiScale(
            gray,
            scaleFactor=1.2,
            minNeighbors=5,
            minSize=(16, 16)
        )
        
        for (x,y,w,h) in faces:
            cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = frame[y:y+h, x:x+w]  
        cv2.imshow('video',frame)
        
        if cv2.waitKey(20) & 0xFF == ord('q'):
            break;
except Exception as e:
    print(e);
cap.release()
cv2.destroyAllWindows()

# build custome database from downloaded photos


import numpy as np
import cv2
from PIL import Image

face_detector = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_default.xml')
image_dir = os.path.join("./", "downloaded_dataset")

try:
    count = 0
    folder_name = input("please Enter Folder Name to create dataset\n")
    
    download_folder_path = os.path.join(image_dir,folder_name)
    
    face_id = int(input("please Enter face_id of this person\n"))
    
    
    created_folder_path = "./images/{folder_n}".format(folder_n = folder_name)
    
    print("created folder",os.mkdir(created_folder_path))

#     print(download_folder_path) ---> ./downloaded_dataset\priyanka

#    We are expecting there are only images of single person in this folder no subfolders
    for root, dirs, files in os.walk(download_folder_path):
        if(root is not download_folder_path):
            break
            
        print(root)
        print(len(files))
        
        count = 0
        
        for image_file in files:
            image_file_path = os.path.join(download_folder_path,image_file)
            PIL_img = Image.open(image_file_path).convert('L') # grayscale
            gray = np.array(PIL_img,'uint8')
            
            faces = face_detector.detectMultiScale(
                gray,
                scaleFactor=1.3,
                minNeighbors=5,
#                 minSize=(24, 24)
            )
            if(len(faces)>=2):
                print(image_file_path)
#             minSize=(16, 16)
            for (x,y,w,h) in faces:
                if(w<0 or h<0):
                    continue
                try:
                    cv2.imshow('image', gray[y:y+h,x:x+w])
                except Exception as e:
                    print(pa)
                count+=1
                 
                pa = os.path.join(created_folder_path,str(face_id)+'.'+str(count) + ".jpg")
                try:
                    cv2.imwrite(pa, gray[y:y+h,x:x+w])
                except Exception as e:
                    print(pa)
                count+=1
                                
            
#             cv2.imwrite( + str(face_id) + '.' +  
#                             str(count) + ".jpg", gray[y:y+h,x:x+w])
#             cv2.imshow('image', gray[y:y+h,x:x+w])
            if cv2.waitKey(100) & 0xff==ord('q'):
                cv2.destroyAllWindows()                
                break
        print("\n [INFO] Exiting Program and cleanup stuff")
except Exception as e:
    print("something went wrong")
    print(e)
        
print("found {c}".format(c=count))    
cv2.destroyAllWindows()   
# capture images and make database
import numpy as np
import cv2
from PIL import Image

try:
    cam = cv2.VideoCapture("C:/Users/17211/Desktop/chakri_sample.mp4")
    cam.set(3, 640) # set video width
    cam.set(4, 480) # set video height

    face_detector = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_default.xml')

    folder_name = input("Enter folder name to save photos in it")
    
    print("creating folder {}".format(folder_name))
    created_folder_path = "./images/{folder_n}".format(folder_n = folder_name)
    print("created folder",os.mkdir(created_folder_path))

    face_id = input('\n enter user faceid i.e to save photo as face_id.count')
    print("\n [INFO] Initializing face capture. Look the camera and wait ...")# Initialize individual sampling face count
    count = 0

    while(True):
        ret, img = cam.read()
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_detector.detectMultiScale(
            gray,
            scaleFactor=1.3,
            minNeighbors=5,
        )

        for (x,y,w,h) in faces:
            cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)     
            count += 1
            # Save the captured image into the datasets folder
            cv2.imwrite("images/{}/".format(folder_name) + str(face_id) + '.' +  
                        str(count) + ".jpg", gray[y:y+h,x:x+w])
            cv2.imshow('image', img)
        if cv2.waitKey(100) & 0xff==ord('q'):
            break
        elif count >= 200: # Take 30 face sample and stop video
             break# Do a bit of cleanup
    print("\n [INFO] Exiting Program and cleanup stuff")
except Exception as e:
    print(e)
cam.release()
cv2.destroyAllWindows()
# Training model

face_detector = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_default.xml')
recognizer = cv2.face.LBPHFaceRecognizer_create()
def getImagesAndLabels(path):
    print("getting all face_vectors and face_labels")
    imagePaths = [os.path.join(path,f) for f in os.listdir(path)]     
#     print(len(imagePaths))
    faceSamples=[]
    ids = []
    for root, dirs, files_names in os.walk(path):
        total_files = 0
        detected_faces = 0

        if(root==path):
            continue
        for file in files_names:
            if file.endswith("png") or file.endswith("jpg") or file.endswith("JPG"):
                total_files = total_files+1
                image_path = os.path.join(root, file)
                PIL_img = Image.open(image_path).convert('L') # convert it to grayscale
                img_numpy = np.array(PIL_img,'uint8')

                # print(os.path.split(file)) ('', '1.194.jpg')
                # os.path.split(file)[-1].split(".") --->>> ['1', '194', 'jpg']
                
                label_id = int(os.path.split(file)[-1].split(".")[0])
                faces = face_detector.detectMultiScale(img_numpy) 
                
                for (x,y,w,h) in faces:
                    detected_faces = detected_faces+1
                    faceSamples.append(img_numpy[y:y+h,x:x+w])
                    ids.append(label_id)
        print("root:",root,end='\n')
        print("DIR:",len(dirs),end='\n')
        print("Files:",len(files_names))
        print("total images:"+str(total_files))
        print("detected_faces:" +str(detected_faces))
    return faceSamples,ids

print ("\n [INFO] Training faces. It will take a few seconds. Wait ...")
faces_got,ids = getImagesAndLabels("./dataset/")

recognizer.train(faces_got, np.array(ids))

# Save the model into recognizers/trainer.yml
recognizer.write('recognizers/trainner.yml')

print("Saved the model into recognizers/trainer.yml")
import cv2
import numpy as np
import os 

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('recognizers/trainner.yml')

faceCascade = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_alt.xml')
font = cv2.FONT_HERSHEY_SIMPLEX

id = 0
names = ['', '', '<NAME>',"Puneeth","<NAME>","<NAME>"]

cam = cv2.VideoCapture(0)
cam.set(3, 640) # set video widht
cam.set(4, 480) # set video height

# Define min window size to be recognized as a face
minW = 0.1*cam.get(3)
minH = 0.1*cam.get(4)

while True:
    ret, img =cam.read()
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    
    faces = faceCascade.detectMultiScale( 
        gray,
        scaleFactor = 1.2,
        minNeighbors = 3,
#         minSize = (int(minW), int(minH)),
       )
    for(x,y,w,h) in faces:
        cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)
        id, confidence = recognizer.predict(gray[y:y+h,x:x+w])
                # If confidence is less them 100 ==> "0" : perfect match 
        if (confidence < 100):
            id = names[id]
            confidence = "  {0}%".format(round(100 - confidence))
        else:
            id = "unknown"
            confidence = "  {0}%".format(round(100 - confidence))
        
        cv2.putText(
                    img, 
                    str(id), 
                    (x+5,y-5), 
                    font, 
                    1, 
                    (255,255,255), 
                    2
                   )
        cv2.putText(
                    img, 
                    str(confidence), 
                    (x+5,y+h-5), 
                    font, 
                    1, 
                    (255,255,0), 
                    1
                   )  
    
    cv2.imshow('camera',img) 
    if cv2.waitKey(10) & 0xff==ord("q"):
        break
        # Do a bit of cleanup
print("\n [INFO] Exiting Program and cleanup stuff")
cam.release()
cv2.destroyAllWindows()
# onclick change id's

import os 

image_path = os.path.join("images",input("Enter the Person Folder name"))

id = int(input("enter person id name"))
count = 1
for root,dirs,files in os.walk(image_path):
    for file in files:
        arr = file.split(".") #['1', '1', 'jpg']
        new_file_name = "{0}".format(str(id))+"."+str(count)+".jpg"
        os.rename(os.path.join(image_path,file),os.path.join(image_path,new_file_name))
        count = count +1
print("Successfully changed all")
#         os.rename(file,'
class Recognizer:
    
    def __init__(self):
        pass
    
    def disconnect(self):
        self.socket.disconnect()
    def getImageAndLabels(self,path):
            print("getting all face_vectors and face_labels")
            imagePaths = [os.path.join(path,f) for f in os.listdir(path)]     
        #     print(len(imagePaths))
            faceSamples=[]
            ids = []
            for root, dirs, files_names in os.walk(path):
                total_files = 0
                detected_faces = 0
                if(root==path):
                    continue
                for file in files_names:
                    if file.endswith("png") or file.endswith("jpg") or file.endswith("JPG"):
                        total_files = total_files+1
                        image_path = os.path.join(root, file)
                        PIL_img = Image.open(image_path).convert('L') # convert it to grayscale
                        img_numpy = np.array(PIL_img,'uint8')

                        # print(os.path.split(file)) ('', '1.194.jpg')
                        # os.path.split(file)[-1].split(".") --->>> ['1', '194', 'jpg']

                        label_id = int(os.path.split(file)[-1].split(".")[0])
                        faces = face_detector.detectMultiScale(img_numpy) 

                        for (x,y,w,h) in faces:
                            detected_faces = detected_faces+1
                            faceSamples.append(img_numpy[y:y+h,x:x+w])
                            ids.append(label_id)
                print("root:",root,end='\n')
                print("DIR:",len(dirs),end='\n')
                print("Files:",len(files_names))
                print("total images:"+str(total_files))
                print("detected_faces:" +str(detected_faces))
            return faceSamples,ids
        
    def train(self):
        print("training ")
        # BASE_DIR = os.path.dirname(os.path.abspath(__file__)) this says where is this notebook exactly is..
        image_dir = os.path.join("./", "images")
        
        face_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_default.xml')
        recognizer = cv2.face.LBPHFaceRecognizer_create()
        self.getImageAndLabels(image_dir)
        
        faces_got,ids = getImagesAndLabels("./dataset/")
        recognizer.train(faces_got, np.array(ids))

        # Save the model into recognizers/trainer.yml
        recognizer.write('recognizers/trainner.yml')

        print("Saved the model into recognizers/trainer.yml")
        
    def recognize(self):
        
        recognizer = cv2.face.LBPHFaceRecognizer_create()
        recognizer.read('recognizers/trainner.yml')

        faceCascade = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_alt.xml')
        font = cv2.FONT_HERSHEY_SIMPLEX

        id = 0
        names = ['', '', '<NAME>',"Puneeth","<NAME>","<NAME>"]

        cam = cv2.VideoCapture(0)
        cam.set(3, 640) # set video widht
        cam.set(4, 480) # set video height
        
        while True:
            if self.stop:
                self.socket.disconnect()
                break
                
            ret, img =cam.read()
            gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

            faces = faceCascade.detectMultiScale( 
                gray,
                scaleFactor = 1.2,
                minNeighbors = 3,
               )
            for(x,y,w,h) in faces:
                cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)
                id, confidence = recognizer.predict(gray[y:y+h,x:x+w])
                
                # If confidence is less then 100 ==> "0" : perfect match 
                if (confidence < 100):
                    id = names[id]
                    confidence = "  {0}%".format(round(100 - confidence))
                    sio.emit('push_faces', {'location': random.choice(locations),'name':names[id]}, namespace='/facemap')                    
                    
                else:
                    id = "unknown"
                    confidence = "  {0}%".format(round(100 - confidence))
                    
                cv2.putText(
                            img, 
                            str(id), 
                            (x+5,y-5), 
                            font, 
                            1, 
                            (255,255,255), 
                            2
                           )
                cv2.putText(
                            img, 
                            str(confidence), 
                            (x+5,y+h-5), 
                            font, 
                            1, 
                            (255,255,0), 
                            1
                           )  

            cv2.imshow('camera',img) 
            if cv2.waitKey(10) & 0xff==ord("q"):
                break
                # before ending its complusory we release resources 
        cam.release()
        cv2.destroyAllWindows()
recognizer = Recognizer(sio)
recognizer.train()<jupyter_output>training 
./images<jupyter_text>Recognizer has     * Train     * Recognize<jupyter_code>import cv2 
import pafy

url = input()



#start the video
cap = cv2.VideoCapture(url)
while (True):
    ret,frame = cap.read()
    
    cv2.imshow('frame',frame)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break    

cap.release()
cv2.destroyAllWindows()<jupyter_output>https://scontent-sjc3-1.cdninstagram.com/v/t50.2886-16/28114194_183390445602443_5360236018444996638_n.mp4?efg=eyJ2ZW5jb2RlX3RhZyI6InZ0c192b2RfdXJsZ2VuLjY0MC5mZWVkLiIsInFlX2dyb3VwcyI6IltcImlnX3dlYl9kZWxpdmVyeV92dHNfb3RmXCJdIn0&_nc_ht=scontent-sjc3-1.cdninstagram.com&_nc_cat=111&_nc_ohc=oPhAhasDq1sAX-ssk8k&vs=17913407596100166_2340278657&_nc_vs=HBksFQAYJEdCTDlyQUdMaGwzcXlxWUFBQjRRcTA5Q1ltTktia1lMQUFBRhUAAsgBABUAGCRHR0phcndGcWxrM050RFFCQU5kX0xIMG5fSGRXYmtZTEFBQUYVAgLIAQAoABgAGwGIB3VzZV9vaWwBMBUAACbQxriiqeuEARUCKAJDMywXQENPXCj1wo8YFGRhc2hfYmFzZWxpbmVfNjQwX3YxEQB16gcA&_nc_rid=33960ee7c7&oe=5FFFF789&oh=44222e441c6d7f30fecb3c0824271a43
{'fraction_non_alphanumeric': 0.1211750305997552, 'fraction_numerical': 0.029498164014687883, 'mean_word_length': 1.8784569314778932, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 24, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 7}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '210269', 'license': 'Apache-2.0', 'n_tokens_mistral': 5645, 'n_tokens_neox': 5212, 'n_words': 989, 'path': 'facemap_recognizer/src/.ipynb_checkpoints/facemap_database_notebook-checkpoint.ipynb', 'repo_name': 'PuneethRegonda/realtime_surveillance_system', 'size': '16384'}