Create a row representation of an image from an image array. :param imgArray: ndarray, image data. :return: Row, image as a DataFrame Row with schema==ImageSchema.
(imgArray, origin="")
| 70 | |
| 71 | |
| 72 | def imageArrayToStruct(imgArray, origin=""): |
| 73 | """ |
| 74 | Create a row representation of an image from an image array. |
| 75 | |
| 76 | :param imgArray: ndarray, image data. |
| 77 | :return: Row, image as a DataFrame Row with schema==ImageSchema. |
| 78 | """ |
| 79 | # Sometimes tensors have a leading "batch-size" dimension. Assume to be 1 if it exists. |
| 80 | if len(imgArray.shape) == 4: |
| 81 | if imgArray.shape[0] != 1: |
| 82 | raise ValueError( |
| 83 | "The first dimension of a 4-d image array is expected to be 1.") |
| 84 | imgArray = imgArray.reshape(imgArray.shape[1:]) |
| 85 | imageType = _arrayToOcvMode(imgArray) |
| 86 | height, width, nChannels = imgArray.shape |
| 87 | data = bytearray(imgArray.tobytes()) |
| 88 | return Row(origin=origin, mode=imageType.ord, height=height, |
| 89 | width=width, nChannels=nChannels, data=data) |
| 90 | |
| 91 | |
| 92 | def imageStructToArray(imageRow): |