face_recognition.load_image_file("{}"),什么意思?

face_recognition.load_image_file("{}"), what does it mean?

我是 python 的新手。现在我正在处理 python 相关项目。我在 google 中搜索与项目相关的内容时看到了以下代码。

image = face_recognition.load_image_file("{}")

"{}"load_image_file() 函数中做了什么?另外,我应该何时何地使用它?

如果不了解更多此方法 load_image_file 的工作原理,很难回答。但这里有 2 个有根据的猜测可能是准确的:

  1. {} 将是空 dictionary。字典在 Python 中非常重要和有用,并且与 key:value 对一起使用。例如,以下是比给定的空字典更有用的字典:

    • phone_nums = {'home': '867-5309', 'work': '555-1212'} 将创建一个包含 2 个键('home''work')的字典,这些键与 2 个 phone 数字相关联。例如,phone_nums['home'] 会产生 867-5309.
  2. "{}" 将是空 "Python dictionary" 的 JSON representation。我将 Python 词典放在引号中,因为 JSON 表示的一个关键方面是它可以 "talk" 跨不同的语言。您可以在 Python 中将对象写入 JSON 结构,然后在 Javascript 或许多其他语言中打开它。 (我提到 Javascript 是因为那是 JSON 最初的来源。)


但是回到你的问题,即它在那个方法中做了什么。如果让我猜的话,我会说方法:

  • 查找 JSON 表示中的字符串。
  • 在该表示中寻找一些相关的键名,例如 'filename': 'C:\Users\my_file.jpeg'
    • 如果找不到 'filename' 关键字,则使用默认路径。
      • 在这种情况下,这就是正在发生的事情,也是该代码片段的作者想要的。但是,s/he 不能简单地传递 null 或一个空字符串,如果确实存在 object/dictionary 的 JSON 表示形式的话。不过,这完全是推测。

根据文档:

face_recognition.api.load_image_file(file, mode='RGB')[source]
Loads an image file (.jpg, .png, etc) into a numpy array

Parameters: 
file – image file name or file object to load
mode – format to convert the image to. Only ‘RGB’ (8-bit RGB, 3 channels) and ‘L’ (black and white) are supported.
Returns:    
image contents as numpy array

https://face-recognition.readthedocs.io/en/latest/face_recognition.html#face_recognition.api.load_image_file

因此您需要传递图像路径或对象。 "{}" 向我模拟不完整的代码。