🏠
🌡️ 📆 令和5年6月1日
戻る 進む

🗒️ tensorflow URLを指定して画像を取得 Python3用

⇒#2288@研究ノート;

日時
関係者(共同研究者)

http://amenity.yz.yamagata-u.ac.jp/

http://qiita.com…考にpython3に移植

import tensorflow as tf
import numpy as np
import requests

## 定数
IMG_SIZE = 256 ## 学習させる画像の縦幅横幅
IMG_LENGTH = IMG_SIZE * IMG_SIZE * 3 ## 学習させる画像データ
LABEL_CNT = 11 ## ラベルの種類の数
IMG_DOMAIN = 'ftp.yz.yamagata-u.ac.jp' ## 画像が取得できるURLのドメイン

## 学習に必要な変数の初期化
x = tf.placeholder(tf.float32, shape=[None, IMG_LENGTH])
W = tf.Variable(tf.zeros([IMG_LENGTH, LABEL_CNT]))
b = tf.Variable(tf.zeros([LABEL_CNT]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, shape=[None, LABEL_CNT])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)





## CSVファイルークキューとして設定
queue = tf.train.string_input_producer(['image.csv'])
reader = tf.TextLineReader()
key, val = reader.read(queue)
url, label = tf.decode_csv(val, [[''], [0]])

sess = tf.Session()
sess.run(tf.global_variables_initializer())

## 処理の準備
batch_url, batch_label = tf.train.batch([url, label], batch_size=100)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)






try :
while not coord.should_stop() :
urls, labels = sess.run([batch_url, batch_label])

for url in urls :
#con = http.client.HTTPConnection(IMG_DOMAIN)
#response = con.request('GET', "/pub/camera/AXIS_000/201708021519.jpg")
#r1 = response.getresponse()
#image = r1.read()
r=requests.get(url)
image = r.content

## 画像TensorFlowで処理できるように変換
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.resize_image_with_crop_or_pad(image, IMG_SIZE, IMG_SIZE)
image = tf.reshape(image, [-1])
image = sess.run(image).astype(np.float32) / 255.0

for label in labels :
tmp = np.zeros(LABEL_CNT)
tmp[label] = 1
label = tmp
sess.run(train_step, feed_dict={x: [image], y_: [label]})

finally :
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(accuracy, feed_dict={x: [image], y_: [label]})
coord.request_stop()
coord.join(threads)

器具・消耗品(履歴)
履歴ID概要
サンプル(履歴)
履歴ID概要
装置(履歴)
履歴ID概要

  1 375 🖱 テキスト画像変換

テキストは、文字コードの羅列です。 文字コードを 表示や印刷するには、フォントを使って画像にします。


QRコード
https://edu.yz.yamagata-u.ac.jp/developer/Asp/Youzan/Laboratory/LaboNote/@LaboNote.asp?id=2288


山形大学 データベースアメニティ研究所
〒992-8510 山形県米沢市城南4丁目3-16
3号館(物質化学工学科棟) 3-3301
准教授 伊藤智博
0238-26-3573
http://amenity.yz.yamagata-u.ac.jp/

Copyright ©1996- 2023 Databese Amenity Laboratory of Virtual Research Institute,  Yamagata University All Rights Reserved.