मैं इस ट्यूटोरियल का उपयोग TensorFlow के साथ आरंभ करने के लिए कर रहा हूँ - कवि के लिए TensorFlow।
Retrain.py स्क्रिप्ट का उपयोग करके मॉडल को प्रशिक्षित करने के बाद, मैं वीडियो को वर्गीकृत करने और वीडियो के चलने के दौरान परिणामों को लाइव देखने के लिए retrained_graph.pb का उपयोग करना चाहता हूं।
मैंने जो किया वह वीडियो को पढ़ने के लिए ओपनसीवी का उपयोग करना है जिसे मैं फ्रेम द्वारा फ्रेम वर्गीकृत करना चाहता हूं। यानी एक फ्रेम पढ़ें, इसे सेव करें, इसे खोलें, इसे वर्गीकृत करें और इसे cv2.imshow() का उपयोग करके वर्गीकरण परिणाम के साथ स्क्रीन पर दिखाएं।
यह काम करता है, लेकिन डिस्क से/में फ्रेम के पढ़ने और लिखने के कारण, परिणामी वीडियो पिछड़ा हुआ है।
क्या मैं प्रशिक्षण प्रक्रिया से प्राप्त ग्राफ का उपयोग कर सकता हूं और एक वीडियो को फ्रेम दर फ्रेम पढ़े और सहेजे बिना वर्गीकृत कर सकता हूं?
यह वह कोड है जिसका मैं उपयोग कर रहा हूं -
with tf.Session(graph=graph) as sess:
video_capture = cv2.VideoCapture(video_path)
i = 0
while True:
frame = video_capture.read()[1] # get current frame
frameId = video_capture.get(1) #current frame number
i = i + 1
cv2.imwrite(filename="C:\\video_images\\"+ str(i) +".jpg", img=frame) # write frame image to file
image_data = "C:\\video_images\\" + str(i) + ".jpg"
t = read_tensor_from_image_file(image_data,
input_height=input_height,
input_width=input_width,
input_mean=input_mean,
input_std=input_std)
predictions = sess.run(output_operation.outputs[0], {input_operation.outputs[0]: t})
top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]
scores = []
for node_id in top_k:
human_string = label_lines[node_id]
score = predictions[0][node_id]
scores.append([score, human_string])
#print('%s (score = %.5f)' % (human_string, score))
#print("\n\n")
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, scores[0][1] + " - " + repr(round(scores[0][0], 2)), (10, 50), font, 1, (0, 0, 255), 2, cv2.LINE_AA)
cv2.putText(frame, scores[1][1] + " - " + repr(round(scores[1][0], 2)), (10, 100), font, 1, (0, 0, 255), 2, cv2.LINE_AA)
cv2.imshow("image", frame)
cv2.waitKey(1)
os.remove("C:\\video_images\\" + str(i) + ".jpg")
video_capture.release()
cv2.destroyAllWindows()
धन्यवाद।
2 जवाब
इसे हल करने में कामयाब रहे।
निम्नलिखित में read_tensor_from_image_file संपादित किया और बस इसे image_data के बजाय फ्रेम के साथ खिलाया।
def read_tensor_from_image_file(file_name,
input_height=299,
input_width=299,
input_mean=0,
input_std=255):
input_name = "file_reader"
output_name = "normalized"
if type(file_name) is str:
file_reader = tf.read_file(file_name, input_name)
if file_name.endswith(".png"):
image_reader = tf.image.decode_png(file_reader, channels = 3,
name='png_reader')
elif file_name.endswith(".gif"):
image_reader = tf.squeeze(tf.image.decode_gif(file_reader,
name='gif_reader'))
elif file_name.endswith(".bmp"):
image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader')
else:
image_reader = tf.image.decode_jpeg(file_reader, channels = 3,
name='jpeg_reader')
float_caster = tf.cast(image_reader, tf.float32)
dims_expander = tf.expand_dims(float_caster, 0);
resized = tf.image.resize_bilinear(dims_expander, [input_height,
input_width])
normalized = tf.divide(tf.subtract(resized, [input_mean]),
[input_std])
sess = tf.Session()
result = sess.run(normalized)
elif type(file_name) is np.ndarray:
resized = cv2.resize(file_name, (input_width, input_height),
interpolation=cv2.INTER_LINEAR)
normalized = (resized - input_mean) / input_std
result = normalized
result = array(result).reshape(1, 224, 224, 3)
return result
frame = video_capture.read()[1] # get current frame
float_caster = frame.astype(np.float32)
dims_expander = np.expand_dims(float_caster, axis=0)
resized = cv2.resize(dims_expander,(int(input_width),int(input_height)))
normalized = (resized - input_mean) / input_std
predictions = sess.run(output_operation.outputs[0], {input_operation.outputs[0]: normalized})
केवल read_tensor_from_image_file
को कॉल करने के लिए imwrite
का उपयोग करने के बजाय, फ़्रेम स्वयं प्राप्त करें। इसका आकार बदलें और इसे सामान्य करें। फिर, normalized
को सत्र में पास करें। इस तरह डिस्क के अनावश्यक लिखने/पढ़ने के कार्यों से छुटकारा पाएं।
संबंधित सवाल
नए सवाल
python
पायथन एक बहु-प्रतिमान है, गतिशील रूप से टाइप किया हुआ, बहुउद्देशीय प्रोग्रामिंग भाषा है। यह एक साफ और एक समान वाक्यविन्यास सीखने, समझने और उपयोग करने के लिए त्वरित होने के लिए डिज़ाइन किया गया है। कृपया ध्यान दें कि अजगर 2 आधिकारिक तौर पर 01-01-2020 के समर्थन से बाहर है। फिर भी, संस्करण-विशिष्ट पायथन सवालों के लिए, [अजगर -२.०] या [अजगर -३.x] टैग जोड़ें। पायथन वेरिएंट (जैसे, ज्योथन, PyPy) या लाइब्रेरी (उदा।, पांडस और न्यूमपी) का उपयोग करते समय, कृपया इसे टैग में शामिल करें।