tensorflow opencv 基本操作

2021-09-25 10:58:36 字數 3979 閱讀 3078

import tensorflow as tf

import cv2

#開始程式,進行測試

hello=tf.constant('hello world')

sess=tf.session()

print(sess.run(hello))

#的讀取和展示

#的讀取和寫入。儲存為png格式

#有失真壓縮

cv2.imwrite('baby-less-quality.jpg',img,(cv2.imwrite_jpeg_quality,80)) #壓縮引數範圍為:0-100 有失真壓縮

#無失真壓縮

cv2.imwrite('baby-less-quality.png',img,(cv2.imwrite_png_compression,1))#壓縮引數範圍為:0-9 有失真壓縮

#畫素讀寫

#大小計算 長*高*3(三原色)*8(8位256色)

(b,g,r)=img[100,100]

print(b,g,r)#列印乙個畫素的三原色值

for i in range(1,300):

img[10+i,100]=(0,0,255) #寫入一條直線

cv2.imshow('imgage',img)

cv2.waitkey(0)

#常量 變數

import tensorflow as tf

data1=tf.constant(2.5,dtype=tf.float32)

data2=tf.variable(3,name='b')

print(data1)

print(data2)

''' 塊注釋

sess=tf.session()

init=tf.global_variables_initializer()

sess.run(init)

print(sess.run(data1))

print(sess.run(data2))

'''init=tf.global_variables_initializer()

sess=tf.session()

with sess:

sess.run(init)

print(sess.run(data2))

#四則運算 加減乘除

import tensorflow as tf

data1=tf.constant(6)

data2=tf.constant(2)

dataadd=tf.add(data1,data2)

datamul=tf.multiply(data1,data2)

datasub=tf.subtract(data1,data2)

datadiv=tf.divide(data1,data2)

with tf.session() as sess:

print(sess.run(dataadd))

print(sess.run(datasub))

print(sess.run(datamul))

print(sess.run(datadiv))

#變數 加減乘除

data1=tf.variable(6)

data2=tf.variable(2)

dataadd=tf.add(data1,data2)

datamul=tf.multiply(data1,data2)

datasub=tf.subtract(data1,data2)

datadiv=tf.divide(data1,data2)

init=tf.global_variables_initializer()

with tf.session() as sess:

sess.run(init)

print(sess.run(dataadd))

print(sess.run(datasub))

print(sess.run(datamul))

print(sess.run(datadiv))

# 矩陣運算基礎

import tensorflow as tf

data1=tf.placeholder(tf.float32)

data2=tf.placeholder(tf.float32)

dataadd=tf.add(data1,data2)

with tf.session() as sess:

print(sess.run(dataadd,feed_dict=))

print('end')

import tensorflow as tf

data1=tf.constant([[5,6]])

data2=tf.constant([[1,2],

[3,4]])

data3=tf.constant([[3,3]])

data4=tf.constant([[0,2],

[1,2],

[2,2]])

with tf.session() as sess:

print(sess.run(data4))#列印整體

print(sess.run(data4[0]))#列印某一行

print(sess.run(data4[:,0]))#列印列

print(sess.run(data4[0,0]))

print('end!')

print(sess.run(data1))#列印整體

print(sess.run(data1[0]))#列印某一行

print(sess.run(data1[:,0]))#列印列

print(sess.run(data1[0,0]))

datamul=tf.matmul(data1,data2)

dataadd=tf.add(data1,data3)

with tf.session() as sess:

print(sess.run(datamul))

print(sess.run(dataadd))

mat0=tf.constant([[0,0,0],[0,0,0]])

mat1=tf.zeros([2,3])

mat2=tf.ones([2,3])

mat3=tf.fill([2,3],15)

print(mat1)

mat4=tf.zeros_like(mat3)

mat5=tf.ones_like(mat3)

mat6=tf.linspace(1.5,2.5,17)

mat7=tf.random_uniform([2,3],-1,17)

with tf.session() as sess:

print(sess.run(mat0))

print(sess.run(mat1))

print(sess.run(mat2))

print(sess.run(mat3))

print(sess.run(mat4))

print(sess.run(mat5))

print(sess.run(mat6))

print(sess.run(mat7))

TensorFlow OpenCV程式設計環境的搭建

1.1.1 anaconda介紹 anaconda指的是乙個開源的python發行版本,其包含了conda python等180多個科學包及其依賴項,其中也包含tensorflow框架 opencv庫 以及python常用的庫 numpy matplotlib 等,因此使用anaconda搭建開發環...

Selenium webdriver基本操作1

usr bin env python coding utf 8 from selenium import webdriver import time print 瀏覽器最大化 生成driver物件 driver webdriver.firefox 將瀏覽器最大化 driver.maximize wi...

Elasticsearch 基本CRUD操作

如果指定了id使用指定的id,如果沒有指定則自動生成,如果id重複,則先進行刪除再新建並對文件版本加1 post goods doc post goods doc 1 post goods doc 1 需指定id,會公升級版本,如果id已經存在則報錯 post goods create 1 get ...