Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detection in video #25

Open
kaimonhtun opened this issue Jun 16, 2021 · 2 comments
Open

Detection in video #25

kaimonhtun opened this issue Jun 16, 2021 · 2 comments

Comments

@kaimonhtun
Copy link

How to test detection for video file?

@duanzhiihao
Copy link
Owner

Could you provide more information?

If you mean testing with no metric evaluation, you can just test on every single frame like in

RAPiD/api.py

Line 51 in e56ac87

def detect_one(self, **kwargs):

If you mean testing with metric evaluation, I guess you need to write your own script to do this.

@seino-tsuyoshi
Copy link

seino-tsuyoshi commented Jul 26, 2021

For example,

from api import Detector
import numpy as np
import cv2

from PIL import Image
def cv2pil(imgCV):
    imgCV_RGB = cv2.cvtColor(imgCV, cv2.COLOR_BGR2RGB)
    imgPIL = Image.fromarray(imgCV_RGB)
    return imgPIL  

detector = Detector(model_name='rapid',
                    weights_path='./weights/pL1_MWHB1024_Mar11_4000.ckpt')

cap = cv2.VideoCapture('videos/test.mp4')

while(cap.isOpened()):
    # フレームを取得
    ret, frame = cap.read()

    if ret == False:
        break

    np_img = detector.detect_one(pil_img=cv2pil(frame),
                    input_size=1024, conf_thres=0.3,
                    return_img=True)
    new_image = np.array(np_img, dtype=np.uint8)
    np_img = cv2.cvtColor(new_image, cv2.COLOR_RGB2BGR)
    # フレームを表示
    cv2.imshow("Frame", np_img)

    # qキーが押されたら途中終了
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

However, this code has duplicate value error in "pil_img" on api.py.
So you can modify the code below,

api.py #L73

RAPiD/api.py

Line 72 in fcb764a

detections = self._predict_pil(img, **kwargs)

to

detections = self._predict_pil(**kwargs)

and L116

RAPiD/api.py

Lines 116 to 130 in fcb764a

def _predict_pil(self, pil_img, **kwargs):
'''
Args:
pil_img: PIL.Image.Image
input_size: int, input resolution
conf_thres: float, confidence threshold
'''
input_size = kwargs.get('input_size', self.input_size)
conf_thres = kwargs.get('conf_thres', self.conf_thres)
assert isinstance(pil_img, Image.Image), 'input must be a PIL.Image'
assert input_size is not None, 'Please specify the input resolution'
assert conf_thres is not None, 'Please specify the confidence threshold'
# pad to square
input_img, _, pad_info = utils.rect_to_square(pil_img, None, input_size, 0)

to

    def _predict_pil(self, **kwargs):
        '''
        Args:
            pil_img: PIL.Image.Image
            input_size: int, input resolution
            conf_thres: float, confidence threshold
        '''
        input_size = kwargs.get('input_size', self.input_size)
        conf_thres = kwargs.get('conf_thres', self.conf_thres)
        assert isinstance(kwargs.get('pil_img', None), Image.Image), 'input must be a PIL.Image'
        assert input_size is not None, 'Please specify the input resolution'
        assert conf_thres is not None, 'Please specify the confidence threshold'

        # pad to square
        input_img, _, pad_info = utils.rect_to_square(kwargs.get('pil_img', None), None, input_size, 0)

It works for me.

https://youtu.be/6h19o3Sj0UA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants