def drawAll(img, buttonList): for button in buttonList: x, y = button.pos w, h = button.size cvzone.cornerRect(img, (button.pos[0], button.pos[1], button.size[0], button.size[1]), 20, rt=0) cv2.rectangle(img, button.pos, (x + w, y + h), (255, 0, 255), cv2.FILLED) cv2.putText(img, button.text, (x + 20, y + 65), cv2.FONT_HERSHEY_PLAIN, 4, (255, 255, 255), 4) return img
class DragRect(): def __init__(self, posCenter, size=[200, 200]): self.posCenter = posCenter self.size = size def update(self, cursor): cx, cy = self.posCenter w, h = self.size # If the index finger tip is in the rectangle region if cx - w // 2 < cursor[0] < cx + w // 2 and \ cy - h // 2 < cursor[1] < cy + h // 2: self.posCenter = cursor rectList = [] for x in range(5): rectList.append(DragRect([x * 250 + 150, 150])) while True: success, img = cap.read() img = cv2.flip(img, 1) img = detector.findHands(img) lmList, _ = detector.findPosition(img) if lmList: l, _, _ = detector.findDistance(8, 12, img, draw=False) print(l) if l < 30: cursor = lmList[8] # index finger tip landmark # call the update here for rect in rectList: rect.update(cursor) ## Draw solid # for rect in rectList: # cx, cy = rect.posCenter # w, h = rect.size # cv2.rectangle(img, (cx - w // 2, cy - h // 2), # (cx + w // 2, cy + h // 2), colorR, cv2.FILLED) # cvzone.cornerRect(img, (cx - w // 2, cy - h // 2, w, h), 20, rt=0) ## Draw Transperency imgNew = np.zeros_like(img, np.uint8) for rect in rectList: cx, cy = rect.posCenter w, h = rect.size cv2.rectangle(imgNew, (cx - w // 2, cy - h // 2), (cx + w // 2, cy + h // 2), colorR, cv2.FILLED) cvzone.cornerRect(imgNew, (cx - w // 2, cy - h // 2, w, h), 20, rt=0) out = img.copy() alpha = 0.5 mask = imgNew.astype(bool) out[mask] = cv2.addWeighted(img, alpha, imgNew, 1 - alpha, 0)[mask] cv2.imshow("Image", out) cv2.waitKey(1)
def drawAll(img, buttonList): for button in buttonList: x, y = button.pos # print("x", type(x), "y", type(y)) # print("button type", type(button.pos)) w, h = button.size cvzone.cornerRect( img, (button.pos[0], button.pos[1], button.size[0], button.size[1]), 20, rt=0) # print("button pos", type(button.pos)) # break cv2.rectangle(img, button.pos, (x + w, y + h), (255, 0, 255), cv2.FILLED) # cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) #workd cv2.putText(img, button.text, (x + 20, y + 65), cv2.FONT_HERSHEY_PLAIN, 4, (255, 255, 255), 4) return img
import cvzone import cv2 cap = cv2.VideoCapture(0) detector = cvzone.HandDetector() while True: # Get image frame success, img = cap.read() # Find the hand and its landmarks img = detector.findHands(img, draw=False) lmList, bbox = detector.findPosition(img, draw=False) if bbox: # Draw Corner Rectangle cvzone.cornerRect(img, bbox) # Display cv2.imshow("Image", img) cv2.waitKey(1)