<filename>anothercam.py
import cv2
import numpy as np
def scan_camera():
    cam = cv2.VideoCapture(0)

    cv2.namedWindow("test")

    img_counter = 0
    count= 0
    faces = ["FRONT","UP","BOTTOM","LEFT","RIGHT","BACK"]
    esconfirm = 1
    whole = False
    centers = []
    while True:
        face_text =faces[count]
        ret, frame = cam.read()
        height, width, _ = frame.shape
        
        cv2.getTextSize(face_text, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)[0]
        #add padding
        twidth = min(height*0.7,width*0.7)
        theight = min(height*0.7,width*0.7)
        sqwidth = (twidth/4)
        sqheight = (theight/4)
        widths = [sqwidth,sqwidth*2,sqwidth*3]
        heights = [sqheight,sqheight*2,sqheight*3]
        cv2.putText(frame, str(face_text),(int((width/2)-0.05*width),int(height-(0.1*height))), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0),2,cv2.LINE_AA)
        cv2.putText(frame, str(face_text),(int((width/2)-0.05*width),int(height-(0.1*height))), cv2.FONT_HERSHEY_SIMPLEX, 1, (245,254,254),1,cv2.LINE_AA)
        for s_height in heights:
            for s_width in widths:
                w_ = int(s_width+(0.2*width))
                h_ = int(s_height+(0.2*height))
                centers.append({"face":face_text,"center":(w_,h_),"radius":int(sqwidth*0.3)})
                cv2.circle(frame, (w_,h_), int(sqwidth*0.3), (0,0,0), thickness=3, lineType=8, shift=0)
                cv2.circle(frame, (w_,h_), int(sqwidth*0.3), (255,255,255), thickness=2, lineType=8, shift=0)

        cv2.imshow("test", frame)
        if not ret:
            break
        k = cv2.waitKey(1)

        if k%256 == 27:
            # ESC pressed
            if whole:
                print("Escape hit, closing...")
                break
            else:
                if esconfirm <=1:
                    print("Not every face scanned, press again to force close")
                    esconfirm+=1
                else:
                    print("forcing exit")
                    break

        elif k%256 == 32:
            # SPACE pressed
            esconfirm = 1
            img_name = "FACE_{}.png".format(face_text)
            Z = frame.reshape((-1,3))
            Z = np.float32(Z)

            # define criteria, number of clusters(K) and apply kmeans()
            criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
            K = 16
            ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
            # Now convert back into uint8, and make original image
            center = np.uint8(center)
            res = center[label.flatten()]
            res2 = res.reshape((frame.shape))
            cv2.imwrite(img_name, res2)
            print("{} written!".format(img_name))
            img_counter += 1
            if count == 5:
                count=0
                whole = True
            else:
                count+=1

    cam.release()

    cv2.destroyAllWindows()
    return(centers)
    #print(centers)
centers = scan_camera()
for center in centers:
    temp = cv2.imread("FACE_"+str(center["face"])+".png")
    

<filename>anothercam.py
import cv2
import numpy as np
def scan_camera():
    cam = cv2.VideoCapture(0)

    cv2.namedWindow("test")

    img_counter = 0
    count= 0
    faces = ["FRONT","UP","BOTTOM","LEFT","RIGHT","BACK"]
    esconfirm = 1
    whole = False
    centers = []
    while True:
        face_text =faces[count]
        ret, frame = cam.read()
        height, width, _ = frame.shape
        
        cv2.getTextSize(face_text, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)[0]
        #add padding
        twidth = min(height*0.7,width*0.7)
        theight = min(height*0.7,width*0.7)
        sqwidth = (twidth/4)
        sqheight = (theight/4)
        widths = [sqwidth,sqwidth*2,sqwidth*3]
        heights = [sqheight,sqheight*2,sqheight*3]
        cv2.putText(frame, str(face_text),(int((width/2)-0.05*width),int(height-(0.1*height))), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0),2,cv2.LINE_AA)
        cv2.putText(frame, str(face_text),(int((width/2)-0.05*width),int(height-(0.1*height))), cv2.FONT_HERSHEY_SIMPLEX, 1, (245,254,254),1,cv2.LINE_AA)
        for s_height in heights:
            for s_width in widths:
                w_ = int(s_width+(0.2*width))
                h_ = int(s_height+(0.2*height))
                centers.append({"face":face_text,"center":(w_,h_),"radius":int(sqwidth*0.3)})
                cv2.circle(frame, (w_,h_), int(sqwidth*0.3), (0,0,0), thickness=3, lineType=8, shift=0)
                cv2.circle(frame, (w_,h_), int(sqwidth*0.3), (255,255,255), thickness=2, lineType=8, shift=0)

        cv2.imshow("test", frame)
        if not ret:
            break
        k = cv2.waitKey(1)

        if k%256 == 27:
            # ESC pressed
            if whole:
                print("Escape hit, closing...")
                break
            else:
                if esconfirm <=1:
                    print("Not every face scanned, press again to force close")
                    esconfirm+=1
                else:
                    print("forcing exit")
                    break

        elif k%256 == 32:
            # SPACE pressed
            esconfirm = 1
            img_name = "FACE_{}.png".format(face_text)
            Z = frame.reshape((-1,3))
            Z = np.float32(Z)

            # define criteria, number of clusters(K) and apply kmeans()
            criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
            K = 16
            ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
            # Now convert back into uint8, and make original image
            center = np.uint8(center)
            res = center[label.flatten()]
            res2 = res.reshape((frame.shape))
            cv2.imwrite(img_name, res2)
            print("{} written!".format(img_name))
            img_counter += 1
            if count == 5:
                count=0
                whole = True
            else:
                count+=1

    cam.release()

    cv2.destroyAllWindows()
    return(centers)
    #print(centers)
centers = scan_camera()
for center in centers:
    temp = cv2.imread("FACE_"+str(center["face"])+".png")
    

#add padding # ESC pressed # SPACE pressed # define criteria, number of clusters(K) and apply kmeans() # Now convert back into uint8, and make original image #print(centers)