小弟我在做利用azure的內容仲裁api去識別google雲端硬碟資料夾11裡是否具有言犯性或不應出現的影像,如果我直接貼單張圖片的url會成功但改成讀取資料夾時就會呈現最後一張圖那樣請各位大神幫幫我下方是我程式碼
import os
import requests
from PIL import Image
from io import BytesIO
import http.client, urllib.request, urllib.parse, urllib.error
import json
apikey = "ac03a2e65462430c86f772320f5f64e8"
Host = 'https://re-content4.cognitiveservices.azure.com/'
image_folder_path = "/content/drive/MyDrive/11" # 替換為您的圖片資料夾路徑
headers = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': apikey
}
params = urllib.parse.urlencode({
'CacheImage': '1',
})
parsed_url = urllib.parse.urlparse(Host)
conn = http.client.HTTPSConnection(parsed_url.netloc)
for filename in os.listdir(image_folder_path):
image_path = os.path.join(image_folder_path, filename)
image = Image.open(image_path)
image.show()
with open(image_path, 'rb') as image_file:
image_data = image_file.read()
body = {
"DataRepresentation": "URL",
"Value": "https://drive.google.com/your_image_link",
"ContentModeration": {
"AdultClassifier": "True",
"RacyClassifier": "True",
"GoryClassifier": "True",
"UnderageClassifier": "True"
}
}
conn = http.client.HTTPSConnection(parsed_url.netloc)
try:
conn.request("POST", "/contentmoderator/moderate/v1.0/ProcessImage/Evaluate?%s" % params, json.dumps(body, ensure_ascii=False).encode('utf-8'), headers)
response = conn.getresponse()
data = response.read()
print(data)
print()
img_evaluate = json.loads(data)
print("是否具有言犯性或不應出現的影像:" + str(img_evaluate["IsImageAdultClassified"]))
print("是否具有色情內容:" + str(img_evaluate["IsImageRacyClassified"]))
conn.close()
except Exception as e:
print(str(e))