深度学习 07_2 虚拟计算机(不使用摄像头, 通过将视频聊天的方式实现)

核心代码及逻辑

如何在没有摄像头的情况下进行视频截取(通过qq视频, 然后进行截图), 通过将视频聊天窗口放置于桌面某个位置, 然后对某个位置进行实时截图, 然后实现虚拟计算机的项目.
核心代码(qq视频截图显示):

import numpy as np
from PIL import ImageGrab
import cv2

# 屏幕截图, 然后显示
img = np.array(ImageGrab.grab(bbox = (1500, 620, 1850, 1080)))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

print(img.shape)
print(img)

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

1.1  打开摄像头,显示每一帧图片

import cv2

# 创建类button:
class Button:
    def __init__(self, pos, width, height, value):
        self.pos = pos
        self.width = width
        self.height = height
        self.value = value
        
    def draw(self, img):
        # 绘制计算器的小格子
        # 先画灰色的矩形,再画矩形的边框
#         cv2.rectangle(img, (self.pos[0], self.pos[1]), (self.pos[0] + self.width, self.pos[1] + self.height), (225, 225, 225), -1)|
        cv2.rectangle(img, (self.pos[0], self.pos[1]), (self.pos[0] + self.width, self.pos[1] + self.height), (255, 255, 255), 1)
        cv2.putText(img, self.value, (self.pos[0] + 10, self.pos[1] + 25), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)
        
    def check_click(self, x, y):
        if self.pos[0] < x < self.pos[0] + self.width and self.pos[1] < y < self.pos[1] + self.height:
            cv2.rectangle(img, (self.pos[0] + 3, self.pos[1] + 3), 
                         (self.pos[0] + self.width - 3, self.pos[1] + self.height - 3), (255, 255, 255), -1)
            cv2.putText(img, self.value, (self.pos[0] + 5, self.pos[1] + 20), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 0), 3)
            return True
        else:
            return False

1.2 实现虚拟计算机

import numpy as np
from PIL import ImageGrab
import cv2
import time
from cvzone.HandTrackingModule import HandDetector

button_values = [['7', '8', '9', '*'],
                ['4', '5', '6', '-'],
                ['1', '2', '3', '+'],
                ['0', '/', '.', '=']]
button_list = []
for x in range(4):
    for y in range(4):
        x_pos = 180 + x * 30
        y_pos = 200 + y * 40
        button = Button((x_pos, y_pos), 30, 40, button_values[y][x])
        button_list.append(button)

# 创建hand detector 追踪手
detector = HandDetector(maxHands = 1, detectionCon= 0.8)

my_equation = ''
delay_counter = 0
        
while True:
    # 获取截图,通过视频的方式进行实现要求
    screen = np.array(ImageGrab.grab(bbox = (1500, 620, 1850, 1080)))
    img = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)
    # 显示窗口太小
    # 显示的视角与实际相反
    img = cv2.flip(img, 1) # 1 > 0, 表示左右反转
    
     # 检测手
    hands, img = detector.findHands(img, flipType=False)
#     print(img.shape)
#     img = img.reshape(1920, 1080, 3)
    
    for button in button_list:
        button.draw(img)

    # 创建显示结果的窗口
    w1 = 180
    h1 = 160
#     cv2.rectangle(img, (w1, h1), (w1 + 120, h1 + 40), (225, 225, 225), -1)
    cv2.rectangle(img, (w1, h1), (w1 + 120, h1 + 40), (255, 255, 255), 1)
    
    # print(hands)
    if hands:
        # 找到食指和中指,计算两者的距离
        lmlist = hands[0]['lmList']
        length, _, img = detector.findDistance(lmlist[8], lmlist[12], img)
        # 根据食指和中指的距离判断
        
        x, y = lmlist[8]
        # 外部定义一个true 
        
        if length < 10 and delay_counter == 0:
            if my_value == '=':
                my_equation = ''
            delay_counter = 1
#             love = False
            for i, button in enumerate(button_list):
                if button.check_click(x, y):
                    # 说明是正确点击,显示点击数字
                    my_value = button_values[int(i % 4)][int(i / 4)]
                    # 如果是等号,要计算,如果不是,一直显示
                    if my_value == '=':
                        try:
                            my_equation = str(eval(my_equation))[:11]
                        except Exception:
                            # 非法的数学逻辑
                            # 重新输入
                            my_equation = ''
                    else:
                        # 字符串的拼接
                        my_equation += my_value
                        # time.sleep(1)
    # 重置delay_counter
    if delay_counter != 0:
        delay_counter += 1
        if  delay_counter > 10:
            delay_counter = 0

    cv2.putText(img, my_equation, (190 ,190), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)
    cv2.imshow('img', img)
    key = cv2.waitKey(1)
    if key == ord('q'):
        break
    elif key == ord('c'):
        # 清空输出
        my_equation = ''

cv2.waitKey(0)
cv2.destroyAllWindows()

你可能感兴趣的:(深度学习,深度学习,opencv,python)