如何在终止后导入另一个python文件



这是一个图像识别代码,允许用户登录并使用另一个Python脚本。如果人脸被识别,则中断人脸检测,然后允许登录用户访问另一个Python脚本。代码不能同时运行,一个应该运行,然后另一个启动。下面是示例代码

Login = 0
while True:
# ...
# code that sets Login = 1
# ...
if Login == 1:
break 
import file

不幸的是,代码在没有提供对其他文件的访问权限的情况下中断,它只是中断并终止代码。请帮我解决这个问题。

这是因为import file语句位于break之后。一旦到达break,它所在的循环将被保留,并且在同一循环内该break之后的行将不执行。

只需删除此处的中断

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

break语句脱离for循环,因此您需要在与for相同的级别使用import file,并且if语句应在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

您需要在中断之前切换所需的函数,因为它完全退出并放弃循环的其余部分

if Login==1:

import file
break

最新更新