标签:meta path encoding __name__ oat dataset show bsp 处理
1. 通过人脸检测获取海报中人脸个数及颜色均值。
2. 分析海报人脸个数,颜色均值和评分的关系。
main.py
1 # -*- coding: utf-8 -*- 2 3 import pandas as pd 4 from tools import get_n_face, get_color_mean 5 import matplotlib.pyplot as plt 6 7 # 数据集路径 8 dataset_path = ‘./dataset/movie_metadata.csv‘ 9 10 # 是否第一次运行 11 is_first_run = False 12 13 14 def run_main(): 15 """ 16 主函数 17 """ 18 if is_first_run: 19 # 第一次运行程序 20 data_df = pd.read_csv(dataset_path, nrows=100) 21 22 # 记录海报中人脸个数 23 print(‘海报人脸检测:‘) 24 data_df[‘n_face‘] = data_df[‘movie_imdb_link‘].apply(get_n_face) 25 26 # 保存结果 27 data_df.to_csv(‘./imdb_face.csv‘, index=False, encoding=‘utf-8‘) 28 29 # 记录海报的平均像素值(这里代表图像的主要颜色,可通过颜色的表达方式替代) 30 print(‘海报像素均值计算:‘) 31 data_df[‘color_mean‘] = data_df[‘movie_imdb_link‘].apply(get_color_mean) 32 33 # 保存结果 34 data_df.to_csv(‘./imdb_face_color.csv‘, index=False, encoding=‘utf-8‘) 35 36 data_df = pd.read_csv(‘./imdb_face_color.csv‘) 37 # 分析结果 38 # 人脸个数和评分的关系 39 # 去除无效人脸 40 data_df = data_df[data_df[‘n_face‘] != -1] 41 face_score = data_df[‘imdb_score‘].groupby(data_df[‘n_face‘]).mean() 42 face_score.name = ‘Score‘ 43 face_score.index.name = ‘Number of Face‘ 44 face_score.plot(kind=‘bar‘) 45 plt.show() 46 47 # 像素均值和评分的关系 48 color_score = data_df[‘imdb_score‘].groupby(data_df[‘color_mean‘]).mean() 49 color_score.name = ‘Score‘ 50 color_score.index.name = ‘Mean of color‘ 51 color_score.plot(kind=‘bar‘) 52 plt.show() 53 54 55 if __name__ == ‘__main__‘: 56 run_main()
tools.py
1 # -*- coding: utf-8 -*- 2 3 from bs4 import BeautifulSoup 4 import urllib.request 5 import cognitive_face as CF 6 from skimage import io 7 import numpy as np 8 9 10 def get_img_link(movie_link): 11 """ 12 通过电影的链接爬取海报的链接 13 """ 14 movie_html = urllib.request.urlopen(movie_link) 15 movie_html_obj = BeautifulSoup(movie_html, ‘html.parser‘, from_encoding=‘utf-8‘) 16 # 获取海报小图的链接 17 small_poster_img_link = movie_html_obj.find(‘div‘, class_=‘poster‘).find(‘img‘)[‘src‘] 18 19 # 获取海报大图的链接 20 big_poster_img_link = small_poster_img_link[:small_poster_img_link.find(‘._V1_‘) + 4] + ‘.jpg‘ 21 22 return big_poster_img_link 23 24 25 def get_n_face(movie_link): 26 """ 27 通过图像链接获取包含的人脸个数 28 """ 29 print(‘正在处理链接:‘, movie_link) 30 img_link = get_img_link(movie_link) 31 Key = ‘xxxxxxxxxx ‘ # 这里请替换成自己申请的key 32 CF.Key.set(Key) 33 n_face = -1 34 try: 35 face_list = CF.face.detect(img_link) 36 n_face = len(face_list) 37 print(‘人脸个数:‘, n_face) 38 except CF.util.CognitiveFaceException: 39 print(‘无效图片‘) 40 return n_face 41 42 43 def round_to_int(x, base=10): 44 """ 45 将数字转换到最近的整数 46 """ 47 return int(base * round(float(x)/base)) 48 49 50 def get_color_mean(movie_link): 51 """ 52 通过图像链接获取其平均像素值 53 """ 54 print(‘正在处理链接:‘, movie_link) 55 img_link = get_img_link(movie_link) 56 image = io.imread(img_link) 57 int_mean_color = round_to_int(np.mean(image)) 58 print(‘像素均值:‘, int_mean_color) 59 return int_mean_color
标签:meta path encoding __name__ oat dataset show bsp 处理
原文地址:https://www.cnblogs.com/chengchengaqin/p/9655585.html