Search code examples
pythonopencvface-recognition

capture and save image in opencv


import cv2
import face_recognition


cap = cv2.VideoCapture(0)


face_locations = []

while True:

    ret, frame = cap.read()


    rgb_frame = frame[:, :, ::-1]


    face_locations = face_recognition.face_locations(rgb_frame)


    for top, right, bottom, left in face_locations:



        cv2.circle(frame,(int((left + right) / 2), top),15,(0, 0, 255), 2)
    cv2.circle(frame,(350 ,  150),5,(0, 255, 0), 1)



    cv2.imshow('Video', frame)


    if cv2.waitKey(25) == 13:
        break


cap.release()
cv2.destroyAllWindows()

Screenshot of the result : enter image description here

Goal:

I need the save the image only if the green circle is inside the red circle and the saved image should not contain the circles.

If the green circle is not inside the red circle, it must not save the image.


Solution

  • Taking the answer to this question as the basis:Check if circle inside circle

    x1, y1 -> Position of red circle

    x2, y2 -> Position of green circle

    c1 -> radius of red circle

    c2 -> radius of green circle

    import math
    

    And after importing then you need to change the following

    #frame without circles
    frameToSave = frame
    
    #add circles to frame in live feed
    cv2.circle(frame,(int((left + right) / 2), top),15,(0, 0, 255), 2)
    cv2.circle(frame,(350 ,  150),5,(0, 255, 0), 1)
    
    x1 = int((left + right) / 2)
    y1 = top
    c1 = 15
    
    x2 = 350
    y2 = 150
    c2 = 5
    
    d = math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))
    
    if c1 > ( d + c2 ):
        print("green circle inside red circle")
        cv2.imwrite(filename,frameToSave)
    else:
        print("green circle not inside or not fully inside red circle")
        cv2.imwrite(filename,frameToSave)
    

    Edited a bit to fulfill the goal in the comments (live feed with circles, saved image without circles)