0

I want to change the NumPy array from [x0 y0 x1 y1] to be [(y0, x0) (x1, y1)].

I already tried many things but still not found the right way.

This was my code:

import glob
import os
import cv2
import time
import dlib
import face_detection
import numpy as np
import inspect

print(inspect.getsource(dlib.get_frontal_face_detector()))

print(dlib.__file__) 

def draw_faces(im, bboxes):
    for bbox in bboxes:
        x0, y0, x1, y1 = [int(_) for _ in bbox]
        #cv2.rectangle(im, (x0, y0), (x1, y1), (0, 0, 255), 2)
        image = cv2.rectangle(im, pt1=(x0, y0), pt2=(x1, y1), color=(0, 0, 255), thickness=4)       
        conv = np.array(((x0, y0), (x1, y1)))
        
if __name__ == "__main__":
    impaths = "images"
    impaths = glob.glob(os.path.join(impaths, "*.jpg"))
    detector = face_detection.build_detector(
        "DSFDDetector",
        max_resolution=1080
    )
    for impath in impaths:
        if impath.endswith("out.jpg"): continue
        im = cv2.imread(impath)
        print("Processing:", impath)
        t = time.time()
        dets = detector.detect(
            im[:, :, ::-1]
        )[:, :4]

        conv = np.array(dets, dtype=int)
        conv1= np.split(conv, [1,2])
                    
        draw_faces(im, dets)
        print(conv1)
    
        print(f"Detection time: {time.time()- t:.3f}")
        
        imname = os.path.basename(impath).split(".")[0]
        output_path = os.path.join(
            os.path.dirname(impath),
            f"{imname}_out.jpg"
        )

        cv2.imwrite(output_path, im)

    

The item I want to change comes from face_detection.build_detector ()

2
  • Making sure it's not a typo -- you want to flip the x and y, but only for the first tuple? Commented Mar 13, 2021 at 17:39
  • Thank you for your prompt response, Yes it's. that's necessary for next step program. Commented Mar 13, 2021 at 18:12

1 Answer 1

1

Example for converting NumPy array into a list of two tuples:

a = np.array((1, 2, 3, 4))
b = [tuple(a[0:2]), tuple(a[2:4])]

I hope I understand your question (the code you posted in not minimal reproducible sample).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.