質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

378閲覧

YouTubeで再生されやすいサムネの特徴をディープラーニングで解析したい

first

総合スコア0

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2022/11/09 13:43

前提

YouTubeで再生されやすいサムネの特徴をディープラーニングで解析したいと考えています。
YouTubeAPIを用いて、サムネイルを取得するところまでは進んだのですが、画像の前処理の部分でエラーがでてしまいました。エラーの内容からして、画像がきちんと読み込めてないのかと思いますが修正方法がわかりません。

実現したいこと

画像がきちんと読み込めていて、#画像データセットを学習用とテスト用に分割したい

発生している問題・エラーメッセージ

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-11-bb8a76257b78> in <module> 10 y = keras.utils.to_categorical(y, num_classes) 11 #画像データセットを学習用とテスト用に分割 ---> 12 x_train, x_test, y_train, y_test, z_train, z_test = train_test_split(x, y, z, test_size=0.33, random_state= 3) 13 #学習用データセットをモデルの学習でそのまま使用する用と検証用に分割 14 x_train_train, x_train_val, y_train_train, y_train_val, z_train_train, z_train_val = train_test_split(x_train, y_train, z_train, test_size=0.1, random_state = 3) 1 frames /usr/local/lib/python3.7/dist-packages/sklearn/model_selection/_split.py in _validate_shuffle_split(n_samples, test_size, train_size, default_test_size) 2099 "With n_samples={}, test_size={} and train_size={}, the " 2100 "resulting train set will be empty. Adjust any of the " -> 2101 "aforementioned parameters.".format(n_samples, test_size, train_size) 2102 ) 2103 ValueError: With n_samples=0, test_size=0.33 and train_size=None, the resulting train set will be empty. Adjust any of the aforementioned parameters.

該当のソースコード

search_response = youtube.search().list( part='snippet', #検索クエリ q='ゲーム実況', #視聴回数の多い順 order='viewCount', type='video', #50件 maxResults=50, #アップロード日が2020/07/01以降 publishedAfter='2020-07-01T00:00:00Z', #アップロード日が2020/12/01以前 publishedBefore='2020-12-01T00:00:00Z' ) output = youtube.search().list( part='snippet', q='ゲーム実況', order='viewCount', type='video', maxResults=50, publishedAfter='2020-07-01T00:00:00Z', publishedBefore='2020-12-01T00:00:00Z' ).execute() #ループ回数 num = 20 #動画情報を格納するリスト video_list = [] for i in range(num): video_list = video_list + output['items'] search_response = youtube.search().list_next(search_response,output) output=search_response.execute() import pandas as pd #統計情報を取得する関数 def get_statistics(id): statistics = youtube.videos().list(part = 'statistics', id = id).execute()['items'][0]['statistics'] return statistics #フィルタリングする視聴回数の値 HighViewCount = 1000000 df = pd.DataFrame(video_list) df1 = pd.DataFrame(list(df['id']))['videoId'] df2 = pd.DataFrame(list(df['snippet']))[['channelTitle','publishedAt','channelId','title','description']] df3 = pd.DataFrame(list(pd.DataFrame(list(pd.DataFrame(list(df['snippet']))['thumbnails']))['high']))['url'] ddf = pd.concat([df1, df2, df3], axis = 1) df_static = pd.DataFrame(list(ddf['videoId'].apply(lambda x : get_statistics(x)))) df_output = pd.concat([ddf,df_static], axis = 1) df_output['viewCount'] = df_output['viewCount'].astype(int) #視聴回数で動画をフィルタリング df_highview = df_output[df_output['viewCount']>=HighViewCount] #サムネ取得 import requests df_highview = df_highview.drop_duplicates() df_highview = df_highview.reset_index(drop=True) df_loop = df_highview for i in range(len(df_loop)): #URLを入力して画像そのものを取得 response = requests.get(df_loop.loc[i, 'url']) image = response.content filename = './image_' + str(i) + '.jpg' with open(filename, "wb") as f: f.write(image) import glob import PIL import keras from keras.preprocessing import image #リサイズする画像サイズ input_shape = (256, 256, 3) #クラス数 num_classes = 2 #画像データ x = [] #ラベル(1:正例, 0:負例) y = [] #画像のファイル名 z = [] mkdir positive_image mkdir negative_image image_list_positive = glob.glob('positive/image_?.jpg') for f in image_list_positive: x.append(image.img_to_array(image.load_img(f, target_size=input_shape[:2]))) y.append(1) z.append(f) image_list_negative = glob.glob('negative/image_?.jpg') for f in image_list_negative: x.append(image.img_to_array(image.load_img(f, target_size=input_shape[:2]))) y.append(0) z.append(f) #画像の前処理 import numpy as np from keras.utils import plot_model, to_categorical from sklearn.model_selection import train_test_split x = np.asarray(x) x /= 255 y = np.asarray(y) #ラベルをカテゴリカル変数へ変換 y = keras.utils.to_categorical(y, num_classes) #画像データセットを学習用とテスト用に分割 x_train, x_test, y_train, y_test, z_train, z_test = train_test_split(x, y, z, test_size=0.33, random_state= 3) #学習用データセットをモデルの学習でそのまま使用する用と検証用に分割 x_train_train, x_train_val, y_train_train, y_train_val, z_train_train, z_train_val = train_test_split(x_train, y_train, z_train, test_size=0.1, random_state = 3)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

bsdfan

2022/11/10 05:50 編集

とってきた画像を保存しているのはカレントディレクトリですが、読み込もうとしているのは positive と negative というディレクトリの中になってます。どうにかして、画像を、振り分ける必要があるのでは?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問