公子如玉世无双 发表于 2020-12-2 00:43

抄的代码运行出错

本帖最后由 公子如玉世无双 于 2020-12-2 22:24 编辑

import re
import base64
import os
import requests
import random
import time

API_KEY = '1EzS3XaR5ctis3KOkqls4R8M'
SECRET_KEY = 'LBEvxLgNr9AHGGCpsXLAxr31e9HpfFse'


def listdir(path, path_name):
    for file in os.listdir(path):
      file_path = os.path.join(path, file)
      if os.path.isdir(file_path):
            listdir(file_path, path_name)
      else:
            path_name.append(file_path)


def assess(img):
    import base64
    img = open(path, 'rb')
    pic = base64.b64encode(img.read())
    img.close()
    base64 = str(pic, 'utf-8')
    print(base64)
    request_url = 'https://aip.baidubce.com/rest/2.0/face/v3/detect'
    params = {'image': base64, 'image_type': 'BASE64',
            'face_field': 'faceshape,facetype,beauty,'}
    header = {'Content-Type': 'application/json'}
    request_url = request_url + '?access_token' + access_token
    response = requests.post(url=request_url, data=params, headers=header)
    img_json = response.json()
    print(img_json)
    score = img_json['result']['face_list']['beauty']
    print(score)
    return score


headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
star = input("请输入明星名字后按回车键!")
star_url = "https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1606835337612_R&pv=&ic=0&nc=1&z=0&hd=0&latest=0©right=0&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&sid=&word=" + star

try:
    response = requests.get(star_url, headers=headers)
    pic_url_list = re.findall('"thumbURL":"(.*?)"', response.text, re.S)
except Exception as e:
    print('获取地址失败:%s' % e)
if len(pic_url_list) == 0:
    print('获取图片失败')
else:
    print(pic_url_list)

    for pic_url, i in zip(pic_url_list, range(len(pic_url_list) + 1)):
      response = requests.get(pic_url, headers=headers)
      dir_name = star
      if not os.path.exists(dir_name):
            os.mkdir(dir_name)
      with open(dir_name + '/' + star + str(i) + '.jpg', 'wb') as f:
            f.write(response.content)

path_name = []
path = star
all_path = []
listdir(path, path_name)
for path in path_name:
    print(path)
    all_path.append(path)
print(all_path)

host_url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + API_KEY + '&client_secret=' + SECRET_KEY
response = requests.get(host_url)
time.sleep(1)
if response:
    print(response.json())
    print(type(response.json()))
    access_token = response.json()['access_token']
    print(access_token)

all_score = 0
count = 0
for path in all_path:
    try:
      score = assess(path)
      all_score += score
      count += 1
    except Exception as e:
      print(e)
average = all_score/count

bf = "bf.jpg"
bf_score = assess(bf)

add = random.randrange(((80 - average)), (100 - average))

if bf_score < average:
    print(star + "的平均颜值评分为")
    print(average, '分/100分')
    print("你的颜值评分为")
    print(average + add, '分/100分')
elif bf_score > average:
    print(star + "没有你帅气!")
    print(star + "的平均颜值评分为")
    print(average, '分/100分')
    print("你的颜值评分为")
    print(bf_score, '分/100分')
else:
    print(star + "的平均颜值评分为")
    print(average, '分/100分')
    print("你的颜值评分为")
    print(bf_score, '分/100分')
    print("你们一样帅气!")


这是抄的创造太阳的代码,但是运行的时候
Traceback (most recent call last):
File "C:/Users/Administrator/新建文件夹/test12.py", line 91, in <module>
    average = all_score/count
ZeroDivisionError: division by zero

这是哪里出错了
里面的百度api key和密码都是真实的,希望大佬们帮忙解决一下

袁昌旺 发表于 2020-12-2 00:57

all_score/count


这个/用来做什么的?

一片小朵朵 发表于 2020-12-2 01:06

这上面不是有报错,count值为0. 你检查一下path目录(明星名字) 里面的文件啊

RS水果 发表于 2020-12-2 01:13

0不能被作为除数count为0
你好好想想

xieemengxin 发表于 2020-12-2 07:15

count 不能为0   
for path in all_path:
    try:
      score = assess(path)
      all_score += score
      count += 1
    except Exception as e:
      print(e)
average = all_score/count


大概率 access(path)失败

公子如玉世无双 发表于 2020-12-2 07:35

xieemengxin 发表于 2020-12-2 07:15
count 不能为0   
for path in all_path:
    try:


这是什么原因?没有权限吗?

w92vv 发表于 2020-12-2 07:43

71行的结果是啥

公子如玉世无双 发表于 2020-12-2 07:48

本帖最后由 公子如玉世无双 于 2020-12-2 07:49 编辑

w92vv 发表于 2020-12-2 07:43
71行的结果是啥
['范冰冰\\范冰冰0.jpg', '范冰冰\\范冰冰1.jpg', '范冰冰\\范冰冰10.jpg', '范冰冰\\范冰冰11.jpg', '范冰冰\\范冰冰12.jpg', '范冰冰\\范冰冰13.jpg', '范冰冰\\范冰冰14.jpg', '范冰冰\\范冰冰15.jpg', '范冰冰\\范冰冰16.jpg', '范冰冰\\范冰冰17.jpg', '范冰冰\\范冰冰18.jpg', '范冰冰\\范冰冰19.jpg', '范冰冰\\范冰冰2.jpg', '范冰冰\\范冰冰20.jpg', '范冰冰\\范冰冰21.jpg', '范冰冰\\范冰冰22.jpg', '范冰冰\\范冰冰23.jpg', '范冰冰\\范冰冰24.jpg', '范冰冰\\范冰冰25.jpg', '范冰冰\\范冰冰26.jpg', '范冰冰\\范冰冰27.jpg', '范冰冰\\范冰冰28.jpg', '范冰冰\\范冰冰29.jpg', '范冰冰\\范冰冰3.jpg', '范冰冰\\范冰冰4.jpg', '范冰冰\\范冰冰5.jpg', '范冰冰\\范冰冰6.jpg', '范冰冰\\范冰冰7.jpg', '范冰冰\\范冰冰8.jpg', '范冰冰\\范冰冰9.jpg']

codyy 发表于 2020-12-2 08:59

all_score = 0
count = 0
...
average = all_score/count

0/0 当然出错了

minibeetuaman 发表于 2020-12-2 09:07

异常结束你没有退出,而是直接执行下面的操作
页: [1] 2
查看完整版本: 抄的代码运行出错