Welcome to AI View Depth camera repository
7.3D object recognition
7.3D object recognition
1. 3D Object Recognition
3D object recognition: recognizable objects include: ['Shoe ',' Chair ',' Cup ',' Camera '], totaling 4 categories; Click the 'f' button to switch to recognizing objects.
2. Start
Input following command:
cd /home/yahboom/orbbec_ws/src/yahboomcar_mediapipe/scriptspython3 08_Objectron.py

3. Code
Code path:/home/yahboom/orbbec_ws/src/yahboomcar_mediapipe/scripts/08_Objectron.py
x#!/usr/bin/env python3# encoding: utf-8import mediapipe as mpimport cv2 as cvimport time
class FaceDetector: def __init__(self, minDetectionCon=0.5): self.mpFaceDetection = mp.solutions.face_detection self.mpDraw = mp.solutions.drawing_utils self.facedetection = self.mpFaceDetection.FaceDetection(min_detection_confidence=minDetectionCon)
def findFaces(self, frame): img_RGB = cv.cvtColor(frame, cv.COLOR_BGR2RGB) self.results = self.facedetection.process(img_RGB) bboxs = [] if self.results.detections: for id, detection in enumerate(self.results.detections): bboxC = detection.location_data.relative_bounding_box ih, iw, ic = frame.shape bbox = int(bboxC.xmin * iw), int(bboxC.ymin * ih), \ int(bboxC.width * iw), int(bboxC.height * ih) bboxs.append([id, bbox, detection.score]) frame = self.fancyDraw(frame, bbox) cv.putText(frame, f'{int(detection.score[0] * 100)}%', (bbox[0], bbox[1] - 20), cv.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 2) return frame, bboxs
def fancyDraw(self, frame, bbox, l=30, t=10): x, y, w, h = bbox x1, y1 = x + w, y + h cv.rectangle(frame, (x, y),(x + w, y + h), (255, 0, 255), 2) # Top left x,y cv.line(frame, (x, y), (x + l, y), (255, 0, 255), t) cv.line(frame, (x, y), (x, y + l), (255, 0, 255), t) # Top right x1,y cv.line(frame, (x1, y), (x1 - l, y), (255, 0, 255), t) cv.line(frame, (x1, y), (x1, y + l), (255, 0, 255), t) # Bottom left x1,y1 cv.line(frame, (x, y1), (x + l, y1), (255, 0, 255), t) cv.line(frame, (x, y1), (x, y1 - l), (255, 0, 255), t) # Bottom right x1,y1 cv.line(frame, (x1, y1), (x1 - l, y1), (255, 0, 255), t) cv.line(frame, (x1, y1), (x1, y1 - l), (255, 0, 255), t) return frame
if __name__ == '__main__': capture = cv.VideoCapture(0) capture.set(6, cv.VideoWriter.fourcc('M', 'J', 'P', 'G')) capture.set(cv.CAP_PROP_FRAME_WIDTH, 640) capture.set(cv.CAP_PROP_FRAME_HEIGHT, 480) print("capture get FPS : ", capture.get(cv.CAP_PROP_FPS)) pTime, cTime = 0, 0 face_detector = FaceDetector(0.75) while capture.isOpened(): ret, frame = capture.read() # frame = cv.flip(frame, 1) frame,_ = face_detector.findFaces(frame) if cv.waitKey(1) & 0xFF == ord('q'): break cTime = time.time() fps = 1 / (cTime - pTime) pTime = cTime text = "FPS : " + str(int(fps)) cv.putText(frame, text, (20, 30), cv.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 1) cv.imshow('frame', frame) capture.release() cv.destroyAllWindows()