def checkSpaces(): spaces = 0 for pos in posList: x, y = pos w, h = width, height imgCrop = imgThres[y:y + h, x:x + w] count = cv2.countNonZero(imgCrop) if count < 900: color = (0, 200, 0) thic = 5 spaces += 1 else: color = (0, 0, 200) thic = 2 cv2.rectangle(img, (x, y), (x + w, y + h), color, thic) cv2.putText(img, str(cv2.countNonZero(imgCrop)), (x, y + h - 6), cv2.FONT_HERSHEY_PLAIN, 1, color, 2) cvzone.putTextRect(img, f'Free: {spaces}/{len(posList)}', (50, 60), thickness=3, offset=20, colorR=(0, 200, 0))
def checkParkingSpace(imgPro): spaceCounter = 0 for pos in posList: x, y = pos imgCrop = imgPro[y:y + height, x:x + width] # cv2.imshow(str(x * y), imgCrop) count = cv2.countNonZero(imgCrop) if count < 900: color = (0, 255, 0) thickness = 5 spaceCounter += 1 else: color = (0, 0, 255) thickness = 2 cv2.rectangle(img, pos, (pos[0] + width, pos[1] + height), color, thickness) cvzone.putTextRect(img, str(count), (x, y + height - 3), scale=1, thickness=2, offset=0, colorR=color) cvzone.putTextRect(img, f'Free: {spaceCounter}/{len(posList)}', (100, 50), scale=3, thickness=5, offset=20, colorR=(0, 200, 0))
qNo = 0 qTotal = len(dataAll) while True: success, img = cap.read() img = cv2.flip(img, 1) hands, img = detector.findHands(img, flipType=False) if qNo < qTotal: mcq = mcqlist[qNo] img, bbox = cvzone.putTextRect(img, mcq.question, [60, 100], 1, 1, offset=20, border=3) img, bbox1 = cvzone.putTextRect(img, mcq.choice1, [60, 200], 1, 1, offset=20, border=3) img, bbox2 = cvzone.putTextRect(img, mcq.choice2, [230, 200], 1, 1, offset=20, border=3) img, bbox3 = cvzone.putTextRect(img,
success, img = cap.read() imgText = np.zeros_like(img) img, faces = detector.findFaceMesh(img, draw=False) if faces: face = faces[0] pointLeft = face[145] pointRight = face[374] w, _ = detector.findDistance(pointLeft, pointRight) W = 6.3 # Finding distance f = 840 d = (W * f) / w print(d) cvzone.putTextRect(img, f'Depth: {int(d)}cm', (face[10][0] - 100, face[10][1] - 50), scale=2) for i, text in enumerate(textList): singleHeight = 20 + int((int(d / sen) * sen) / 4) scale = 0.4 + (int(d / sen) * sen) / 100 cv2.putText(imgText, text, (50, 50 + (i * singleHeight)), cv2.FONT_ITALIC, scale, (255, 255, 255), 2) imgStacked = cvzone.stackImages([img, imgText], 2, 1) cv2.imshow("Image", imgStacked) cv2.waitKey(1)
cv2.line(img, leftLeft, leftRight, (0, 200, 0), 3) ratio = int((lenghtVer / lenghtHor) * 100) ratioList.append(ratio) if len(ratioList) > 3: ratioList.pop(0) ratioAvg = sum(ratioList) / len(ratioList) if ratioAvg < 35 and counter == 0: blinkCounter += 1 color = (0, 200, 0) counter = 1 if counter != 0: counter += 1 if counter > 10: counter = 0 color = (255, 0, 255) cvzone.putTextRect(img, f'Blink Count: {blinkCounter}', (50, 100), colorR=color) imgPlot = plotY.update(ratioAvg, color) img = cv2.resize(img, (640, 360)) imgStack = cvzone.stackImages([img, imgPlot], 2, 1) else: img = cv2.resize(img, (640, 360)) imgStack = cvzone.stackImages([img, img], 2, 1) cv2.imshow("Image", imgStack) cv2.waitKey(25)
success, img = cap.read() # print(img.shape) hands = detector.findHands(img, draw=False) if hands: lmList = hands[0]['lmList'] x1, y1 = lmList[5] x2, y2 = lmList[17] x, y, w, h = hands[0]['bbox'] distance = np.sqrt(((y2 - y1) ** 2 + (x2 - x1) ** 2)) A, B, C = coff distanceCm = A * (distance ** 2) + B * distance + C # print(distanceCm) cvzone.putTextRect(img, f'{int(distanceCm)} cm', (x + 5, y - 50)) cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 255), 3) if distanceCm <= 70: cv2.putText(img, "Hand Detected", (50, 80), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 5) if distanceCm < 68: break else: cv2.putText(img, "Hand Not Detected", (50, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 5) cv2.imshow("Window", img)
if hands: lmList = hands[0]['lmList'] x, y, w, h = hands[0]['bbox'] x1, y1 = lmList[5] x2, y2 = lmList[17] distance = int(math.sqrt((y2 - y1)**2 + (x2 - x1)**2)) A, B, C = coff distanceCM = A * distance**2 + B * distance + C # print(distanceCM, distance) if distanceCM < 40: if x < cx < x + w and y < cy < y + h: counter = 1 cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 255), 3) cvzone.putTextRect(img, f'{int(distanceCM)} cm', (x + 5, y - 10)) if counter: counter += 1 color = (0, 255, 0) if counter == 3: cx = random.randint(100, 1100) cy = random.randint(100, 600) color = (255, 0, 255) score += 1 counter = 0 # Draw Button cv2.circle(img, (cx, cy), 30, color, cv2.FILLED) cv2.circle(img, (cx, cy), 10, (255, 255, 255), cv2.FILLED) cv2.circle(img, (cx, cy), 20, (255, 255, 255), 2)
ratio = (lengthVer/lengthHor)*100 ratioList.append(ratio) if len(ratioList) > 3: ratioList.pop(0) ratioAvg = sum(ratioList)/len(ratioList) if ratioAvg < 40 and counter == 0: blinkCounter += 1 counter = 1 color = (0, 200, 0) if counter != 0: counter += 1 if counter > 10: counter = 0 color = (255, 0, 255) cvzone.putTextRect(img, f'Blink Count: {blinkCounter}', (50, 50), scale=2, colorR=color) imgPlot = plotY.update(ratioAvg, color) # cv2.imshow("ImagePlot", imgPlot) imgStack = cvzone.stackImages([img, imgPlot], 2, 1) else: imgStack = cvzone.stackImages([img, img], 2, 1) cv2.imshow("Image", imgStack) cv2.waitKey(1)
def update(self, imgMain, currentHead): if self.gameOver: cvzone.putTextRect(imgMain, "Game Over", [300, 400], scale=7, thickness=5, offset=20) cvzone.putTextRect(imgMain, f'Your Score: {self.score}', [300, 550], scale=7, thickness=5, offset=20) else: px, py = self.previousHead cx, cy = currentHead self.points.append([cx, cy]) distance = math.hypot(cx - px, cy - py) self.lengths.append(distance) self.currentLength += distance self.previousHead = cx, cy # Length Reduction if self.currentLength > self.allowedLength: for i, length in enumerate(self.lengths): self.currentLength -= length self.lengths.pop(i) self.points.pop(i) if self.currentLength < self.allowedLength: break # Check if snake ate the Food rx, ry = self.foodPoint if rx - self.wFood // 2 < cx < rx + self.wFood // 2 and \ ry - self.hFood // 2 < cy < ry + self.hFood // 2: self.randomFoodLocation() self.allowedLength += 50 self.score += 1 print(self.score) # Draw Snake if self.points: for i, point in enumerate(self.points): if i != 0: cv2.line(imgMain, self.points[i - 1], self.points[i], (0, 0, 255), 20) cv2.circle(imgMain, self.points[-1], 20, (0, 255, 0), cv2.FILLED) # Draw Food imgMain = cvzone.overlayPNG( imgMain, self.imgFood, (rx - self.wFood // 2, ry - self.hFood // 2)) cvzone.putTextRect(imgMain, f'Score: {self.score}', [50, 80], scale=3, thickness=3, offset=10) # Check for Collision pts = np.array(self.points[:-2], np.int32) pts = pts.reshape((-1, 1, 2)) cv2.polylines(imgMain, [pts], False, (0, 255, 0), 3) minDist = cv2.pointPolygonTest(pts, (cx, cy), True) if -1 <= minDist <= 1: print("Hit") self.gameOver = True self.points = [] # all points of the snake self.lengths = [] # distance between each point self.currentLength = 0 # total length of the snake self.allowedLength = 150 # total allowed Length self.previousHead = 0, 0 # previous head point self.randomFoodLocation() return imgMain