开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
3 Star 3 Fork 0

Lee/human_code

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (2)
master
demo_source
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
master
分支 (2)
master
demo_source
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (2)
master
demo_source
human_code
/
PC_function.py
human_code
/
PC_function.py
PC_function.py 24.35 KB
一键复制 编辑 原始数据 按行查看 历史
李嘉桐 提交于 2019年07月20日 13:22 +08:00 . human
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import time
import os
import sys
import math
from pyzbar import pyzbar
import numpy as np
import cv2
import threading
import img_pro
import datetime
import Serial_Servo_Running as SSR
import LeCmd
import getUsedSpace
from PIL import Image, ImageDraw, ImageFont
print('''
**********************************************************
*******功能:所有玩法的集合,可通过不同指令进行调用********
**********************************************************
----------------------------------------------------------
Official website:http://www.lobot-robot.com/pc/index/index
Online mall:https://lobot-zone.taobao.com/
----------------------------------------------------------
以下指令均需在LX终端使用,LX终端可通过ctrl+alt+t打开,或点
击上栏的黑色LX终端图标。
----------------------------------------------------------
Usage:
-1 | --启动颜色识别玩法
-2 | --启动人脸检测玩法
-3 | --启动智能巡线玩法
-4 | --启动手指个数识别玩法
-5 | --启动人脸个数识别玩法
-6 | --启动二维码识别玩法
-7 | --启动智能监控玩法
-8 | --启动数字计算玩法
----------------------------------------------------------
Example #1:
显示图像,识别红绿蓝三种颜色
python3 PC_function.py -41
----------------------------------------------------------
Example #2:
显示图像,将识别到的人脸框起来
python3 PC_function.py -42
----------------------------------------------------------
Version: --V2.1 2019年06月03日
----------------------------------------------------------
Tips:
* 按下Ctrl+C可中断此次程序运行
----------------------------------------------------------
''')
orgFrame = None
ret = False
stream = "http://127.0.0.1:8080/?action=stream?dummy=param.mjpg"
cap = cv2.VideoCapture(stream)
width, height = 480, 360
font = cv2.FONT_HERSHEY_SIMPLEX
# 数值映射
# 将一个数从一个范围映射到另一个范围
def leMap(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
#找出面积最大的轮廓
#参数为要比较的轮廓的列表
def getAreaMaxContour(contours) :
contour_area_temp = 0
contour_area_max = 0
area_max_contour = None;
for c in contours : #历遍所有轮廓
contour_area_temp = math.fabs(cv2.contourArea(c)) #计算轮廓面积
if contour_area_temp > contour_area_max :
contour_area_max = contour_area_temp
if contour_area_temp > 300: #只有在面积大于300时,最大面积的轮廓才是有效的,以过滤干扰
area_max_contour = c
return area_max_contour, contour_area_max#返回最大的轮廓
def get_image():
global orgFrame
global ret
global cap
while True:
if cap.isOpened():
ret, orgFrame = cap.read()
else:
time.sleep(0.01)
#################################################
# 颜色识别
def cv_color(frame):
global width, height
#颜色的字典
color_range = {'red': [(0,43,46), (6, 255, 255)],
'blue': [(110,43,46), (124, 255,255)],
'green': [(35,43,46), (77, 255, 255)],
}
range_rgb = {'red': (0, 0, 255),
'blue': (255, 0,0),
'green': (0, 255, 0),
}
wd,hg = 320, 240
dispose_frame = cv2.resize(frame, (wd, hg), interpolation = cv2.INTER_CUBIC) #将图片缩放
gs_frame = cv2.GaussianBlur(dispose_frame, (3,3), 0)#高斯模糊
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV)#将图片转换到HSV空间
#分离出各个HSV通道
h, s, v = cv2.split(hsv)
#直方图化
v = cv2.equalizeHist(v)
#合并三个通道
hsv = cv2.merge((h,s,v))
color_list = []
rad = 0
areaMaxContour = 0
max_area = 0
area_max = 0
centerX = 0
centerY = 0
for i in color_range:
mask = cv2.inRange(hsv, color_range[i][0], color_range[i][1])#对原图像和掩模进行位运算
opened = cv2.morphologyEx(mask, cv2.MORPH_OPEN, np.ones((3,3),np.uint8))#开运算
closed = cv2.morphologyEx(opened, cv2.MORPH_CLOSE, np.ones((3,3),np.uint8))#闭运算
(image, contours, hierarchy) = cv2.findContours(closed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)#找出轮廓
areaMaxContour, area_max = getAreaMaxContour(contours)#找出最大轮廓
if areaMaxContour is not None:
if area_max > max_area:#找最大面积
max_area = area_max
color_max = i
areaMaxContour_max = areaMaxContour
if max_area != 0:
((centerX, centerY), rad) = cv2.minEnclosingCircle(areaMaxContour_max) # 获取最小外接圆
centerX = int(leMap(centerX, 0.0, wd, 0.0, width)) # 将数据从0-160 映射到 0-480
centerY = int(leMap(centerY, 0.0, hg, 0.0, height)) # 将数据从0-120 映射到 0-360
rad = int(leMap(rad, 0.0, wd/2, 0.0, 240.0)) # 将数据从0-160 映射到 0-480
if rad >= 10:
cv2.circle(frame, (centerX, centerY), rad, (0, 255, 0), 2)#画出圆心
if color_max == 'red': #红色最大
#print("red")
SSR.thread_runActing('wag_tail', 1)
Color_BGR = range_rgb["red"]
elif color_max == 'green': #绿色最大
SSR.thread_runActing('sit_down', 1)
Color_BGR = range_rgb["green"]
#print("green")
elif color_max == 'blue': #蓝色最大
SSR.thread_runActing('37', 1)
Color_BGR = range_rgb["blue"]
#print("blue")
else:
Color_BGR = (0, 0, 0)
color_max = "Other"
else:
Color_BGR = (0, 0, 0)
color_max = "None"
cv2.putText(frame, "Color: " + color_max, (10, frame.shape[0] - 35), cv2.FONT_HERSHEY_SIMPLEX, 0.65, Color_BGR, 2)
return color_max
#################################################
#################################################
# 人脸检测
face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml')
face_detection_count = 0
def face_detection(frame):
global face_detection_count
dispose_frame = cv2.resize(frame, (160, 120), interpolation=cv2.INTER_LINEAR)
gray = cv2.cvtColor(dispose_frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.2, 8)
for (x, y, w, h) in faces:
cX = int(leMap(x, 0.0, 160.0, 0.0, 480.0)) #0-480
cY = int(leMap(y, 0.0, 120.0, 0.0, 360.0)) #0-360
cW = int(leMap(w, 0.0, 160.0, 0.0, 480.0))
cH = int(leMap(h, 0.0, 120.0, 0.0, 360.0))
cv2.rectangle(frame, (cX, cY), (cX + cW, cY + cH), (255, 0, 0), 2)
if len(faces) >= 1:
face_detection_count += 1
else:
face_detection_count = 0
if face_detection_count >= 5:
# print ('hello')
SSR.thread_runActing('36', 1)
face_detection_count = 0
#################################################
#################################################
# 智能巡线
def turn_left_right(angle):
'''
左转或者右转的动作组,根据实际需要转动的角度转动, angle 为 负数 左转 , 正数 右转
:param angle: 转向的角度
:return:
'''
pwm = int(angle * 4.167 + 500) # 1000(pwm 范围) / 240(舵机角度范围) = 4.166667; 500 中位位置
LeCmd.cmd_i001([50, 8, 1, 500, 2, 775, 3, 500, 4, 125, 5, 500, 6, 775, 7, 500, 8, 125])
time.sleep(0.05)
LeCmd.cmd_i001([50, 8, 1, pwm, 2, 775, 3, 500, 4, 125, 5, pwm, 6, 775, 7, 500, 8, 125])
time.sleep(0.05)
LeCmd.cmd_i001([50, 8, 1, pwm, 2, 875, 3, 500, 4, 125, 5, pwm, 6, 875, 7, 500, 8, 125])
time.sleep(0.05)
LeCmd.cmd_i001([50, 8, 1, 500, 2, 875, 3, 500, 4, 255, 5, 500, 6, 875, 7, 500, 8, 225])
time.sleep(0.05)
LeCmd.cmd_i001([50, 8, 1, 500, 2, 875, 3, pwm, 4, 255, 5, 500, 6, 875, 7, pwm, 8, 225])
time.sleep(0.05)
LeCmd.cmd_i001([50, 8, 1, 500, 2, 875, 3, pwm, 4, 125, 5, 500, 6, 875, 7, pwm, 8, 125])
time.sleep(0.05)
LeCmd.cmd_i001([50, 8, 1, 500, 2, 875, 3, 500, 4, 125, 5, 500, 6, 875, 7, 500, 8, 125])
time.sleep(0.05)
def get_x(img):
'''
范围区域图像内色块的中心坐标X
:param img:
:return:
'''
# 要识别的颜色字典
color_dist = {'red': {'Lower': np.array([0, 50, 50]), 'Upper': np.array([6, 255, 255])},
'blue': {'Lower': np.array([100, 80, 46]), 'Upper': np.array([124, 255, 255])},
'green': {'Lower': np.array([35, 43, 46]), 'Upper': np.array([77, 255, 255])},
'black': {'Lower': np.array([0, 0, 0]), 'Upper': np.array([180, 255, 46])},
}
x = None
# 高斯模糊
gs_frame = cv2.GaussianBlur(img, (5, 5), 0)
# 转换颜色空间
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV)
# 查找颜色
mask = cv2.inRange(hsv, color_dist['black']['Lower'], color_dist['black']['Upper'])
# 腐蚀
mask = cv2.erode(mask, None, iterations=2)
# 膨胀
mask = cv2.dilate(mask, None, iterations=2)
# 查找轮廓
# cv2.imshow('mask', mask)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if len(cnts):
c = max(cnts, key=cv2.contourArea) # 找出最大的区域
area = cv2.contourArea(c)
# 获取最小外接矩形
rect = cv2.minAreaRect(c)
if area >= 500:
xy = rect[0]
xy = int(xy[0]), int(xy[1])
cv2.circle(img, (xy[0], xy[1]), 3, (0, 255, 0), -1)
x = xy[0]
box = cv2.boxPoints(rect)
# 数据类型转换
box = np.int0(box)
# 绘制轮廓
cv2.drawContours(img, [box], 0, (0, 255, 255), 1)
return x
# 三个区域的加权值 从上到下
line_weight = [0.1, 0.35, 0.55]
line_weight_sum = 0
for w in range(len(line_weight)):
line_weight_sum += line_weight[w]
# 机器人应该转的角度
line_deflection_angle = 0
line_cv_ok = False
Exit_thread = False
def run_line():
global line_cv_ok, line_deflection_angle
while True:
if line_cv_ok:
if -25 <= line_deflection_angle <= 25:
SSR.running_action_group('1', 2)
else:
turn_left_right(line_deflection_angle)
time.sleep(0.15)
line_cv_ok = False
else:
time.sleep(0.01)
line_th = threading.Thread(target=run_line)
line_th.setDaemon(True) # 设置为后台线程,这里默认是False,设置为True之后则主线程不用等待子线程
line_th.start()
# 智能巡线
def line_patrol(f):
global line_cv_ok, line_weight, line_weight_sum, line_deflection_angle
# 获取总图像的大小
img_h, img_w = f.shape[:2]
# cv2.imshow('f', f)
up_frame = f[0:65, 0:480]
center_frame = f[145:210, 0:480]
down_frame = f[290:355, 0:480]
up_x = get_x(up_frame)
center_x = get_x(center_frame)
down_x = get_x(down_frame)
if up_x is not None and center_x is not None and down_x is not None and line_cv_ok is False:
centroid_sum = up_x * line_weight[0] + center_x * line_weight[1] + down_x * line_weight[2]
center_pos = (centroid_sum / line_weight_sum) # 求出三个点的加权平均X
deflection_angle = math.atan((center_pos - (img_w / 2)) / (img_h / 2)) # 求出弧度
line_deflection_angle = -math.degrees(deflection_angle) # 转换成角度
line_cv_ok = True
elif center_x is not None and down_x is not None and line_cv_ok is False:
centroid_sum = center_x * line_weight[1] + down_x * line_weight[2]
center_pos = (centroid_sum / (line_weight[1] + line_weight[2])) # 求出三个点的加权平均X
deflection_angle = math.atan((center_pos - (img_w / 2)) / (img_h / 2)) # 求出弧度
line_deflection_angle = -math.degrees(deflection_angle) # 转换成角度
line_cv_ok = True
elif down_x is not None and line_cv_ok is False:
centroid_sum = down_x * line_weight[2]
center_pos = (centroid_sum / line_weight[2]) # 求出三个点的加权平均X
deflection_angle = math.atan((center_pos - (img_w / 2)) / (img_h / 2)) # 求出弧度
line_deflection_angle = -math.degrees(deflection_angle) # 转换成角度
line_cv_ok = True
else:
line_cv_ok = False
#################################################
#################################################
# 手指个数识别
cv_hand_last_hand_num = -1
cv_hand_two_last_hand_num = 0
cv_hand_count = 0
cv_hand_run_one = False
def cv_hand_action(hand_num):
global cv_hand_last_hand_num, cv_hand_count, cv_hand_two_last_hand_num, cv_hand_run_one
if hand_num == cv_hand_last_hand_num:
cv_hand_count += 1
if cv_hand_count >= 8:
cv_hand_count = 0
cv_hand_last_hand_num = -1
if cv_hand_run_one is False:
if hand_num == 0:
#print('run0')
SSR.thread_runActing('0', 1)
if hand_num == 1:
#print('run1')
SSR.thread_runActing('front_back', 1)
if hand_num == 2:
#print('run2')
SSR.thread_runActing('right_left', 1)
if hand_num == 3:
#print('run3')
SSR.thread_runActing('rotate', 1)
if hand_num == 4:
#print('run4')
SSR.thread_runActing('wag_tail', 1)
if hand_num == 5:
#print('run5')
SSR.thread_runActing('37', 1)
cv_hand_run_one = True
cv_hand_two_last_hand_num = hand_num
else:
cv_hand_run_one = False
cv_hand_count = 0
cv_hand_last_hand_num = hand_num
def cv_hand_number(frame):
binary = img_pro.image_process(frame)
# 获取手指个数
hand_num = img_pro.get_hand_number(binary, frame)
# 执行相应的动作组
if hand_num is not None:
cv_hand_action(hand_num)
return hand_num
#################################################
#################################################
# 人脸个数识别
def faces_number(frame):
dispose_frame = cv2.resize(frame, (160, 120), interpolation=cv2.INTER_LINEAR) # 将图片缩放到
gray = cv2.cvtColor(dispose_frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.25, 3)
for (x, y, w, h) in faces:
cX = int(leMap(x, 0.0, 160.0, 0.0, 480.0)) # 将数据从0-160 映射到 0-480
cY = int(leMap(y, 0.0, 120.0, 0.0, 360.0)) # 将数据从0-120 映射到 0-360
cW = int(leMap(w, 0.0, 160.0, 0.0, 480.0))
cH = int(leMap(h, 0.0, 120.0, 0.0, 360.0))
cv2.rectangle(frame, (cX, cY), (cX + cW, cY + cH), (255, 0, 0), 2)
cv2.putText(frame, str(len(faces)), (100, 100), font, 3, (0, 0, 255), 2)
#################################################
#################################################
# 二维码识别
def Qc_code(frame):
global orgframe
barcodeData = None
min_frame = cv2.resize(frame, (160, 120), interpolation=cv2.INTER_LINEAR) # 将图片缩放到
gray_min = cv2.cvtColor(min_frame, cv2.COLOR_BGR2GRAY)
barcodes = pyzbar.decode(gray_min)
for barcode in barcodes:
(x, y, w, h) = barcode.rect
cX = int(leMap(x, 0.0, 160.0, 0.0, 480.0)) # 将数据从0-160 映射到 0-480
cY = int(leMap(y, 0.0, 120.0, 0.0, 360.0)) # 将数据从0-120 映射到 0-360
cW = int(leMap(w, 0.0, 160.0, 0.0, 480.0))
cH = int(leMap(h, 0.0, 120.0, 0.0, 360.0))
cv2.rectangle(frame, (cX, cY), (cX + cW, cY + cH), (0, 0, 255), 2)
barcodeData = barcode.data.decode("utf-8")
frame_cv = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame_pil = Image.fromarray(frame_cv)
draw = ImageDraw.Draw(frame_pil)
fontText = ImageFont.truetype("/usr/share/fonts/chinese/simsun.ttc", 40, encoding='utf-8')
draw.text((cX, cY - 40), barcodeData, (0, 0, 255), font=fontText)
orgframe = cv2.cvtColor(np.asarray(frame_pil), cv2.COLOR_RGB2BGR)
if barcodeData == 'left':
SSR.thread_runActing('7', 6) # 左转
elif barcodeData == 'right':
SSR.thread_runActing('9', 6) # 右转
elif barcodeData == 'straight':
SSR.thread_runActing('1', 5) # 直行
elif barcodeData == 'retreat': # 后退
SSR.thread_runActing('4', 5) # 后退
else:
pass
return barcodeData
#################################################
#################################################
# 智能监控
# 第一帧,用于比较
moving_last_gray = None
moving_first_flag = False # 第一帧标志位
image_number = len(os.listdir("/home/pi/AlienbotPi/video"))
four_cc = cv2.VideoWriter_fourcc(*'XVID')
out_video = cv2.VideoWriter('video/' + str(image_number) + '.avi', four_cc, 20.0, (480, 360))
def moving_objiect_tracking(frame):
global moving_last_gray, moving_first_flag
text = "Unoccupied"
# 转换成灰度图像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 高斯模糊
gray = cv2.GaussianBlur(gray, (21, 21), 0)
if moving_first_flag and moving_last_gray is not None:
# 计算当前帧和第一帧的不同
cv2.accumulateWeighted(gray, moving_last_gray, 0.5)
frameDelta = cv2.absdiff(gray, cv2.convertScaleAbs(moving_last_gray))
# cv2.imshow('frameDelta', frameDelta)
thresh = cv2.threshold(frameDelta, 5, 255, cv2.THRESH_BINARY)[1]
# 扩展阀值图像填充孔洞,然后找到阀值图像上的轮廓
thresh = cv2.dilate(thresh, None, iterations=2)
(_,cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# 遍历轮廓
for c in cnts:
if cv2.contourArea(c) < 5000:
continue
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
text = "Occupied"
moving_first_flag = False
# 保存上一帧
moving_last_gray = gray.copy().astype("float")
moving_first_flag = True
# 在当前帧上写文字以及时间戳
cv2.putText(frame, "Monitor Status: {}".format(text), (10, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
(10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
if text == "Occupied":
space = getUsedSpace.getDiskSpace()
if space > 100.0:
out_video.write(frame) # 如果有画面有动,录制视频
#################################################
#################################################
# 简单数字计算
last_hand_num = -1
count = 0
number_list = []
bit_count = 0
run_one = False
operator = 1 # 运算符 1:+ 2:- 3: * 4:/
operator_list = ['add', 'sub', 'mul', 'div']
bit_time = 0
count_finish = False
def hand_num_action(hand_num):
global last_hand_num, count, bit_count, run_one
global bit_time
if hand_num == last_hand_num:
count += 1
if count >= 5: # 判断五次, 确认手指个数
count = 0
if count_finish is False:
if run_one is False: # 判断是否切换手指
bit_count += 1
number_list.append(hand_num)
# print number_list
run_one = True
else:
if bit_count == 1: # 判断第二个数是否还是一样
bit_time += 1 # 相同手指计时 ,1秒后,手指个数还是相同
if bit_time >= 8: # 大概1.5秒
bit_time = 0
run_one = False
else:
run_one = False
count = 0
bit_time = 0
last_hand_num = hand_num
def digital_computation(frame, key):
global operator, count_finish, number_list, bit_count
global last_hand_num, run_one, count, bit_time
binary = img_pro.image_process(frame)
# 获取手指个数
hand_num = img_pro.get_hand_number(binary, frame)
# 执行相应的动作组
if hand_num is not None:
hand_num_action(hand_num)
else: # 清零
number_list = []
bit_count = 0
last_hand_num = -1
run_one = False
count = 0
bit_time = 0
count_finish = False
if len(number_list) > 0:
cv2.putText(frame, str(number_list[0]), (0, 100), font, 2, (0, 0, 255), 2)
if bit_count == 2:
cv2.putText(frame, str(number_list[1]), (100, 100), font, 2, (0, 0, 255), 2)
cv2.putText(frame, '=', (150, 100), font, 2, (0, 0, 255), 2)
if operator == 1:
cv2.putText(frame, '+', (50, 100), font, 2, (0, 0, 255), 2)
cv2.putText(frame, str(number_list[0] + number_list[1]), (200, 100), font, 2, (0, 0, 255), 2)
elif operator == 2:
cv2.putText(frame, '-', (50, 100), font, 2, (0, 0, 255), 2)
cv2.putText(frame, str(number_list[0] - number_list[1]), (200, 100), font, 2, (0, 0, 255), 2)
elif operator == 3:
cv2.putText(frame, '*', (50, 100), font, 2, (0, 0, 255), 2)
cv2.putText(frame, str(number_list[0] * number_list[1]), (200, 100), font, 2, (0, 0, 255), 2)
elif operator == 4:
if number_list[1] == 0: # 被除数不能为0
cv2.putText(frame, '/', (50, 100), font, 2, (0, 0, 255), 2)
cv2.putText(frame, 'The dividend cannot be 0', (0, 200), font, 1.1, (0, 0, 255), 2)
else:
cv2.putText(frame, '/', (50, 100), font, 2, (0, 0, 255), 2)
cv2.putText(frame, str(number_list[0] / (number_list[1] + 0.0)), (200, 100), font, 2, (0, 0, 255), 2)
count_finish = True
cv2.putText(frame, operator_list[operator - 1], (0, 320), font, 3, (0, 0, 255), 2)
if key == '1':
operator = 1
elif key == '2':
operator = 2
elif key == '3':
operator = 3
elif key == '4':
operator = 4
#################################################
operator = 1
if len(sys.argv) > 1:#对传参长度进行判断
mode = 0
para = sys.argv[1]
if para == "-1":
mode = 1
elif para == "-2":
mode = 2
elif para == "-3":
mode = 3
elif para == "-4":
mode = 4
elif para == "-5":
mode = 5
elif para == "-6":
mode = 6
elif para == "-7":
mode = 7
elif para == "-8":
mode = 8
else:
print("异常:参数输入错误!")
sys.exit()
print('''--程序正常运行中......
''')
th1 = threading.Thread(target = get_image)
th1.setDaemon(True)
th1.start()
while True:
if ret and orgFrame is not None:
try:
ret = False
t1 = cv2.getTickCount()
orgframe = cv2.resize(orgFrame, (480, 360), interpolation=cv2.INTER_LINEAR) # 将图片缩放到
if mode == 1:
cv_color(orgframe)
elif mode == 2:
face_detection(orgframe)
elif mode == 3:
line_patrol(orgframe)
elif mode == 4:
cv_hand_number(orgframe)
elif mode == 5:
faces_number(orgframe)
elif mode == 6:
Qc_code(orgframe)
elif mode == 7:
moving_objiect_tracking(orgframe)
elif mode == 8:
digital_computation(orgframe, operator)
t2 = cv2.getTickCount()
time_r = (t2 - t1) / cv2.getTickFrequency()
fps = 1.0/time_r
cv2.putText(orgframe, "fps:" + str(int(fps)),
(10, orgframe.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)#(0, 0, 255)BGR
cv2.imshow("orgframe", orgframe)
cv2.waitKey(1)
except BaseException as e:
print(e)
continue
else:
time.sleep(0.01)
else:
print("异常:请重新运行,并输入参数!")
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

人形机器人舵机控制,视觉识别模块
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/cnTong/human_code.git
git@gitee.com:cnTong/human_code.git
cnTong
human_code
human_code
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /