计算机视觉基础系列(python与opencv的操作与运用/tensorflow的基础介绍)(四)---tensorflow的变量和常量

tensorflow里面。在运用的时候,变量必须要经过初始化之后才可以运用,常量可以直接运用,但是在运用的时候必须要经过会话Session之后才可以打印出来,而初始化之后也必须要执行会话才可以生效,然后再进行Session会话进行打印,如下代码所示:

import tensorflow as tf
data1 = tf.constant(2, dtype=tf.int32)         # data1定义为常量
data2 = tf.Variable(10, name='var')            # data2定义为变量
sess = tf.Session()
print(sess.run(data1))                         # 经过会话运行打印
init = tf.global_variables_initializer()       # 变量初始化
sess.run(init)                                 # 初始化经过会话生效
print(sess.run(data2))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解如何使用OpenCV-Python实现手势识别的代码,而不涉及Keras。以下是一个基本的手势识别代码示例: 1. 导入必要的库: ```python import cv2 import numpy as np ``` 2. 定义一些常量变量: ```python # 预定义常量 bg = None accumulated_weight = 0.5 font = cv2.FONT_HERSHEY_SIMPLEX # 捕获视频 cap = cv2.VideoCapture(0) # 用于计算帧数的变量 num_frames = 0 # ROI坐标 top, right, bottom, left = 10, 350, 225, 590 ``` 3. 创建一个函数来计算背景平均值: ```python def calc_accum_avg(frame, accumulated_weight): global bg if bg is None: bg = frame.copy().astype("float") return None cv2.accumulateWeighted(frame, bg, accumulated_weight) ``` 4. 创建一个函数来提取手的轮廓: ```python def segment(frame, threshold=25): global bg diff = cv2.absdiff(bg.astype("uint8"), frame) # 应用阈值 thresholded = cv2.threshold(diff, threshold, 255, cv2.THRESH_BINARY)[1] # 找出轮廓 (cnts, _) = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 如果没有轮廓返回None if len(cnts) == 0: return None else: # 根据面积从大到小排序并获取最大的轮廓 segmented = max(cnts, key=cv2.contourArea) return (thresholded, segmented) ``` 5. 开始捕获视频并进行手势识别: ```python while True: # 读取帧 ret, frame = cap.read() # 翻转帧以获得镜像效果 frame = cv2.flip(frame, 1) # 克隆帧 clone = frame.copy() # 获取帧的高度和宽度 (height, width) = frame.shape[:2] # 提取ROI roi = frame[top:bottom, right:left] # 将ROI转换为灰度 gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) # 应用高斯模糊 gray = cv2.GaussianBlur(gray, (7, 7), 0) # 对背景平均值进行计算 calc_accum_avg(gray, accumulated_weight) # 如果有足够的帧进行背景平均值计算,则进行手势识别 if num_frames < 60: cv2.putText(clone, "请等待...", (100, 100), font, 2, (255, 0, 0), 2, cv2.LINE_AA) else: # 提取手的轮廓 hand = segment(gray) # 检查是否成功提取手的轮廓 if hand is not None: # 如果成功提取手的轮廓,则分别获取二值化图像和轮廓 (thresholded, segmented) = hand # 绘制轮廓并显示结果图像 cv2.drawContours(clone, [segmented + (right, top)], -1, (0, 0, 255)) cv2.imshow("Thresholded", thresholded) # 绘制ROI矩形框 cv2.rectangle(clone, (left, top), (right, bottom), (0, 255, 0), 2) # 增加帧数 num_frames += 1 # 显示结果帧 cv2.imshow("Video Feed", clone) # 按'q'键退出 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放资源并销毁所有窗口 cap.release() cv2.destroyAllWindows() ``` 这个代码示例可以实现基本的手势识别。可以根据需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值