Rasberry Pi захватить результаты изменения размера в округленных измерениях - как я могу нарезать результат, используя NumPy? - PullRequest
0 голосов
/ 14 марта 2019

Я пытаюсь сделать захват видео в Rasberry Pi, запрашивая определенную ширину и высоту 300x300.

Вот мой код:

  # Open image.
  with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    camera.framerate = 30

    camera.start_preview()

    try:
      stream = io.BytesIO()
      for foo in camera.capture_continuous(stream,
              format='rgb',
              use_video_port=True,
              resize=(300, 300)):
        stream.truncate()
        stream.seek(0)
        input = np.frombuffer(stream.getvalue(), dtype=np.uint8)

Однако, когда этот кодпри запуске я получаю предупреждение:

/ usr / lib / python3 / dist-packages / picamera / encoders.py: 544: PiCameraResolutionRounded: размер кадра округляется от 300x300 до 320x304 ширина, высота,fwidth, fheight)))

Результирующий тензор находится в 1d, и мне нужно его как 1d для оставшейся части кода.

Как я могу изменить размер этого 1d тензора, чтобы3d, нарежьте его до 300x300, а затем сгладьте обратно до 1d?

1 Ответ

2 голосов
/ 14 марта 2019

Может быть, это может помочь вам

import numpy as np
# This is your iamge, for example.
example_image = np.array([1,1,1,2,2,2,3,3,3])  
print(example_image)
# this is gonna be the same image but in other shape
example_image = np.reshape(example_image,(3,3)) 
print(example_image)

Этот код делает это:

# first print
[1 1 1 2 2 2 3 3 3]
# second print
[[1 1 1]
 [2 2 2]
 [3 3 3]]

хорошо, давайте попробуем использовать некраскованную матрицу, например:

import numpy as np
# This is your iamge, for example.
example_image = np.array([1,1,1,1,2,2,2,2,3,3,3,3])  
print(example_image)
# this is gonna be the same image but in other shape
# 3 Row, 4 Columns
example_image = np.reshape(example_image,(3,4)) 
print(example_image,"\n")
# or maybe this...
example_image = np.array([1,1,1,1,2,2,2,2,3,3,3,3])  
# 3 layers, 2 Row, 2 Columns
example_image = np.reshape(example_image,(3,2,2))   
print(example_image[0], "\n\n" ,example_image[1], "\n\n" ,example_image[2])

Результаты:

#original
[1 1 1 1 2 2 2 2 3 3 3 3]

#First reshape
[[1 1 1 1]
 [2 2 2 2]
 [3 3 3 3]] 

# Second Reshape
[[1 1]
 [1 1]] 

 [[2 2]
 [2 2]] 

 [[3 3]
 [3 3]]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...