Convert numpy ndarray to PIL and and convert it to tensor
def camera(transform):
    capture = cv2.VideoCapture(0)
    while True:
        ret, frame = capture.read()
        cv2.imshow('video', frame)
        # esc
        if cv2.waitKey(1) == 27:
            photo = frame
            break
    capture.release()
    cv2.destroyAllWindows()
    img = Image.fromarray(cv2.cvtColor(photo, cv2.COLOR_BGR2RGB))
    img = img.resize([224, 224], Image.LANCZOS)
    if transform is not None:
        img = transform(img).unsqueeze(0)
    return img

This is my code to get image from the camera,
image_tensor = img.to(device)

And I have an error at the line above...
Traceback (most recent call last):
  File &quot;E:/PycharmProjects/ArsElectronica/image_captioning/sample.py&quot;, line 126, in &lt;module&gt;
    main(args)
  File &quot;E:/PycharmProjects/ArsElectronica/image_captioning/sample.py&quot;, line 110, in main
    caption = Image_Captioning(args, img)
  File &quot;E:/PycharmProjects/ArsElectronica/image_captioning/sample.py&quot;, line 88, in Image_Captioning
    image_tensor = img.to(device)
AttributeError: 'Image' object has no attribute 'to'

The error is like this.
If I have the image as png file and reload it with PIL, it works.
But the one I want is to use the image without saving.
Pls... Someone save me...

You could convert your PIL.Image to torch.Tensor with torchvision.transforms.ToTensor:
    if transform is not None:
        img = transform(img).unsqueeze(0)

    tensor = T.ToTensor()(img)
    return tensor

having imported torchvision.transforms as T
