Home > other >  How to import another python file after terminating
How to import another python file after terminating

Time:03-29

This an image recognition code that allows a user to login and use another Python script. If the face is recognised break the face detection and then allow the logged in user to access another Python script. The code must not run both, one should and then the other one starts. Below is the sample code

Login=0
while True: 
    ret, frame = video_capture.read()

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

    face_locations = fr.face_locations(rgb_frame)
    face_encodings = fr.face_encodings(rgb_frame, face_locations)

    for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):

        matches = fr.compare_faces(known_face_encondings, face_encoding)

        face_distances = fr.face_distance(known_face_encondings, face_encoding)

        best_match_index = np.argmin(face_distances)
        if matches[best_match_index]:
            Print("successfully loged in")
            Login=1
    if Login==1:
        break 
        import file

Unfortunately, the code breaks without providing access to the other file, it just breaks and terminates the code. Please help me fix this.

CodePudding user response:

That's because your import file statement comes after break. Once break is reached, the loop in which it is located will be left, and lines that come after this break inside the same loop are not executed.

CodePudding user response:

just remove the break here

    if Login==1:
        #break  # this makes the if clause to break and doesnt run import file
        import file

CodePudding user response:

The break statement breaks out of the for loop, therefore you need to use import file at the same level as for and if statement should be inside for

Login=0
while True: 
    ret, frame = video_capture.read()

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

    face_locations = fr.face_locations(rgb_frame)
    face_encodings = fr.face_encodings(rgb_frame, face_locations)

    for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):

        matches = fr.compare_faces(known_face_encondings, face_encoding)

        face_distances = fr.face_distance(known_face_encondings, face_encoding)

        best_match_index = np.argmin(face_distances)
        if matches[best_match_index]:
            Print("successfully loged in")
            Login=1
        if Login==1:
            break 
    import file

CodePudding user response:

you need to switch the needed function before the break since it exits completely and abandons the rest of the loop

  if Login==1:
         
        import file
        break
  • Related