Showing posts with label OpenCV. Show all posts
Showing posts with label OpenCV. Show all posts

9/26/2023

QR detector python code

 refer to code:


.

import cv2
#pip install pyzbar
from pyzbar.pyzbar import decode

def scan_qr_code():
# Start the webcam
cap = cv2.VideoCapture(0)
cap.set(3, 640) # Set the width of the window
cap.set(4, 480) # Set the height of the window
while True:
success, img = cap.read()
if not success:
print("Failed to grab frame")
break
# Decode the QR Code
for barcode in decode(img):
# Get the QR Code position
my_data = barcode.data.decode('utf-8')
pts = barcode.polygon
if len(pts) == 4: # If we have 4 points, then we have a QR code
pts2 = barcode.rect
cv2.putText(img, my_data, (pts2[0], pts2[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 255), 2)
for point in pts:
cv2.circle(img, (point[0], point[1]), 5, (0, 255, 0), cv2.FILLED)
# Display the frame
cv2.imshow('QR Code Scanner', img)
if cv2.waitKey(1) & 0xFF == ord('q'): # Press 'q' to quit
break
cap.release()
cv2.destroyAllWindows()

if __name__ == "__main__":
scan_qr_code()

..


you need to install pyzbar


Thank you.

www.marearts.com

🙇🏻‍♂️




9/14/2023

convert you opencv camera calibration yaml file to openSFM camera.json file

 refer to code:


.

from PIL import Image
import json
import yaml
import os
import argparse

def get_image_dimensions(image_path):
with Image.open(image_path) as img:
return img.size

def convert_yaml_to_opensfm_json(yaml_file, json_file, image_path):
image_width, image_height = get_image_dimensions(image_path)

with open(yaml_file, 'r') as f:
calibration_data = yaml.safe_load(f)

# Extract the camera matrix and distortion coefficients
camera_matrix = calibration_data['camera_matrix']
dist_coeff = calibration_data['dist_coeff']

# Compute the normalized focal length
focal_normalized = camera_matrix[0][0] / image_width

# Prepare the JSON data
json_data = {
f"custom_camera {image_width} {image_height} perspective 0.0": {
"projection_type": "perspective",
"width": image_width,
"height": image_height,
"focal": focal_normalized,
"k1": dist_coeff[0][0],
"k2": dist_coeff[0][1]
}
}

# Write the JSON data to file
with open(json_file, 'w') as f:
json.dump(json_data, f, indent=4)

def main():
yaml_file="calibration.yaml"
json_file="./camera_models.json"
image_path="IMG_5306.JPG"
convert_yaml_to_opensfm_json(yaml_file, json_file, image_path)

if __name__ == '__main__':
main()

..


reference :

https://github.com/mapillary/OpenSfM/issues/95

https://opensfm.org/docs/geometry.html#camera-models


Thank you.

www.marearts.com

🙇🏻‍♂️


9/13/2023

OpenCV Camera Calibration source code

refer to code:

.

import numpy as np
import cv2
import glob
import yaml
from icecream import ic
import os

def calibrate_camera(images, chess_box_scale_mm):
# Termination criteria for refining the detected corners
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

# Prepare object points: (0,0,0), (1,0,0), (2,0,0), ..., (9,6,0)
objp = np.zeros((9*6,3), np.float32)
objp[:,:2] = np.mgrid[0:6, 0:9].T.reshape(-1,2) * chess_box_scale_mm# Scale by 7.5mm or 17.5mm or 25mm

# Arrays to store object and image points from all the images
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane

if not images:
raise Exception("No images found in the calibration directory.")

for fname in images:
ic(fname)
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Find the chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (6,9), None)
ic(ret)

# If found, add object points and image points
if ret == True:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
imgpoints.append(corners2)

# Draw and display the corners
cv2.drawChessboardCorners(img, (6,9), corners2, ret)
# Save the image with corners in the same directory but with a .png extension
base_name = os.path.basename(fname)
file_root, file_ext = os.path.splitext(base_name)
save_path = os.path.join(os.path.dirname(fname), f"{file_root}.png")
cv2.imwrite(save_path, img)
cv2.imshow('img', img)
cv2.waitKey(500)


cv2.destroyAllWindows()

if not objpoints or not imgpoints:
raise Exception("Chessboard corners not found in any images.")

# Calibrate the camera using the last value of gray from the loop
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
return ret, mtx, dist, rvecs, tvecs

def main():
path = "./path/to/images/"
images = glob.glob(f'{path}/*.JPG')

ret, mtx, dist, rvecs, tvecs = calibrate_camera(images, 7.5) #chess_box_scale_mm is 7.5 mm

print("Camera matrix: \n", mtx)
print("Distortion coefficients: \n", dist)

# Save to YAML file
data = {'camera_matrix': np.asarray(mtx).tolist(), 'dist_coeff': np.asarray(dist).tolist()}
with open(f"{path}/calibration.yaml", "w") as f:
yaml.dump(data, f)

# Display one of the images after undistortion
img = cv2.imread(images[0]) # Replace with an image from your calibration set
h, w = img.shape[:2]
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w,h), 1, (w,h))

# Undistort
dst = cv2.undistort(img, mtx, dist, None, newcameramtx)

# Crop the image
x, y, w, h = roi
dst = dst[y:y+h, x:x+w]
cv2.imshow('origin Image', img)
cv2.imshow('Undistorted Image', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

if __name__ == "__main__":
main()

.. 


Here is chess board which has 10x7.


png files in same folder are that images succeed for finding pattern.

yaml file will be generated in image folder for camera intrinsic params.


Thank you.

www.marearts.com

🙇🏻‍♂️


4/17/2023

python opencv canny edge source code

 refer to code:


.

import cv2

# Load an image
img = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply Canny edge detection algorithm
edges = cv2.Canny(img, 100, 200)

# Display the input image and the edge map
cv2.imshow('Input Image', img)
cv2.imshow('Canny Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()


..






Thank you.

🙇🏻‍♂️

4/09/2023

Combine Two Videos Side by Side with OpenCV python

 refer to code:

.

import cv2

# Open the two video files
video1 = cv2.VideoCapture('video1.mp4')
video2 = cv2.VideoCapture('video2.mp4')

# Get video properties
width1 = int(video1.get(cv2.CAP_PROP_FRAME_WIDTH))
height1 = int(video1.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps1 = video1.get(cv2.CAP_PROP_FPS)

width2 = int(video2.get(cv2.CAP_PROP_FRAME_WIDTH))
height2 = int(video2.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps2 = video2.get(cv2.CAP_PROP_FPS)

# Check if videos have the same FPS and height
assert fps1 == fps2, "Videos should have the same FPS"
assert height1 == height2, "Videos should have the same height"

# Create a VideoWriter object to save the combined video
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4', fourcc, fps1, (width1 + width2, height1))

while video1.isOpened() and video2.isOpened():
ret1, frame1 = video1.read()
ret2, frame2 = video2.read()

if not ret1 or not ret2:
break

# Concatenate the frames side by side
combined_frame = cv2.hconcat([frame1, frame2])

# Write the combined frame to the output video
out.write(combined_frame)

# Display the combined frame
cv2.imshow('Combined Video', combined_frame)

# Press 'q' to stop the process and close the window
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release the video files and the output video
video1.release()
video2.release()
out.release()

cv2.destroyAllWindows()

..


Thank you.

www.marearts.com

🙇🏻‍♂️

2/19/2023

How to Install OpenCV 4.7 with CUDA, cuDNN, TBB, CUDA Video Codec, and Extra Modules in Linux

 


refer to bash code


.

#!/bin/bash

# Install dependencies
sudo apt-get update
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
sudo apt-get install libcanberra-gtk-module libcanberra-gtk3-module

# Install CUDA 11
wget https://developer.download.nvidia.com/compute/cuda/11.4.0/local_installers/cuda_11.4.0_470.57.02_linux.run
sudo sh cuda_11.4.0_470.57.02_linux.run --silent --toolkit --override
echo 'export PATH=/usr/local/cuda-11.4/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-11.4/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

# Download and extract TBB
wget https://github.com/oneapi-src/oneTBB/releases/download/v2022.0.0/oneapi-tbb-2022.0.0-lin.tgz
tar -xf oneapi-tbb-2022.0.0-lin.tgz
sudo cp -r oneapi-tbb-2022.0.0/lib/* /usr/local/lib/
echo 'export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

# Download and extract OpenCV 4.7 and OpenCV extra modules
wget https://github.com/opencv/opencv/archive/4.7.0.zip
unzip 4.7.0.zip
cd opencv-4.7.0

wget https://github.com/opencv/opencv_contrib/archive/4.7.0.zip
unzip 4.7.0.zip

# Build and install OpenCV 4.7 with CUDA, cuDNN, TBB, CUDA video codec, and OpenCV extra modules
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.7.0/modules -D WITH_CUDA=ON -D WITH_TBB=ON -D WITH_NVCUVID=ON -D WITH_GSTREAMER=ON -D WITH_GSTREAMER_0_10=OFF -D WITH_LIBV4L=ON -D WITH_CUDNN=ON -D CUDA_ARCH_BIN=7.5 ..
make -j$(nproc)
sudo make install
echo 'export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH' >> ~/.bashrc
source ~/.bashrc

# Compile and run the sample code
cd ../../
wget https://raw.githubusercontent.com/spmallick/learnopencv/master/Averaging4kVideo/Averaging4kVideo.cpp
g++ Averaging4kVideo.cpp -o Averaging4kVideo `pkg-config --cflags --libs opencv4`
./Averaging4kVideo

..



thank you.

🙇🏻‍♂️

www.marearts.com

2/16/2023

example code in Python using OpenCV and scikit-learn to build a dataset for video classification

refer to code.


..

import cv2
import os
from sklearn.model_selection import train_test_split

# Define the classes for classification
classes = ['class1', 'class2', 'class3']

# Create a list to store the image paths and labels
data = []

# Loop through the videos and extract frames
for cls in classes:
video_dir = f'data/{cls}/'
for filename in os.listdir(video_dir):
video_path = os.path.join(video_dir, filename)
cap = cv2.VideoCapture(video_path)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame_path = f'frames/{cls}/{filename}_{cap.get(cv2.CAP_PROP_POS_FRAMES)}.jpg'
cv2.imwrite(frame_path, frame)
data.append((frame_path, cls))
cap.release()

# Split the data into training, validation, and test sets
train_val_data, test_data = train_test_split(data, test_size=0.2, stratify=[x[1] for x in data])
train_data, val_data = train_test_split(train_val_data, test_size=0.2, stratify=[x[1] for x in train_val_data])

# Preprocess the images
def preprocess_image(image):
# Resize the image to (224, 224) and normalize the pixel values
image = cv2.resize(image, (224, 224))
image = image.astype('float32') / 255.0
return image

# Load the images and labels into memory
X_train = []
y_train = []
for image_path, label in train_data:
image = cv2.imread(image_path)
image = preprocess_image(image)
X_train.append(image)
y_train.append(label)

X_val = []
y_val = []
for image_path, label in val_data:
image = cv2.imread(image_path)
image = preprocess_image(image)
X_val.append(image)
y_val.append(label)

X_test = []
y_test = []
for image_path, label in test_data:
image = cv2.imread(image_path)
image = preprocess_image(image)
X_test.append(image)
y_test.append(label)

# Train and evaluate the model
# ...

..




In this example, we first define the classes for classification (in this case, 'class1', 'class2', and 'class3'). We then loop through the videos for each class, extract frames from each video, and store the frame paths and labels in a list called data.

Next, we split the data into training, validation, and test sets using train_test_split() from scikit-learn. We stratify the splits to ensure that each set has a proportional number of frames from each class.

We then define a function called preprocess_image() to resize the images to (224, 224) and normalize the pixel values. We load the images and labels into memory for each set using OpenCV, preprocess them using this function, and store them in lists called X_train, y_train, X_val, y_val, X_test, and y_test.

Finally, we can train and evaluate a video classification model using the preprocessed data. The specific code for this step will depend on the model architecture and training strategy you choose to use.


Thank you.

Optical Flow Estimation, How might you calculate the velocity/speed of an object from a ".flo" output

refer to code 

..

import cv2
import numpy as np

# Load the .flo file using OpenCV
flow = cv2.readOpticalFlow('path/to/flow_file.flo')

# Convert displacement vectors to velocity vectors
time_interval = 1.0 # Time interval between frames in seconds
velocity = flow / time_interval

# Load the corresponding frames of the video
frame1 = cv2.imread('path/to/frame1.jpg')
frame2 = cv2.imread('path/to/frame2.jpg')

# Identify object pixels using object detection or segmentation
object_mask = np.zeros(frame1.shape[:2], dtype=np.uint8)
object_mask[...] = 255 # Example: assume the entire frame is the object

# Compute the magnitude of the velocity vector at each object pixel
object_velocity = np.sqrt(np.square(velocity[..., 0]) + np.square(velocity[..., 1])) * object_mask

# Calculate the average speed of the object
object_speed = np.mean(object_velocity[object_mask != 0])

print('Object speed:', object_speed, 'pixels per second')

..




In this example, we first load the .flo file using the cv2.readOpticalFlow() function from OpenCV. We then convert the displacement vectors in the .flo file to velocity vectors by dividing them by the time interval between the two frames. We assume a time interval of 1 second in this example.

Next, we load the corresponding frames of the video and identify the object pixels using a mask. In this example, we assume that the entire frame is the object for simplicity.

We then compute the magnitude of the velocity vector at each object pixel using the np.sqrt() and np.square() functions from NumPy. We apply the object mask to exclude pixels that do not belong to the object.

Finally, we calculate the average speed of the object by averaging the magnitudes of the velocity vectors over all the pixels corresponding to the object using the np.mean() function.

Note that the units of the speed will be in pixels per second, which can be converted to other units (e.g., meters per second) depending on the scale of the video frames. Also, this is just a simple example and you may need to modify it depending on the specific requirements of your application.


Thank you.

1/19/2023

opencv c++ gpumat mat(cpu) memory share

refer to code:

..

size_t frameByteSize = initMat.step[0] * initMat.rows;

#ifndef USE_UNIFIED_MEM
    /* Pinned memory. No cache */
    std::cout << "Using pinned memory" << std::endl;
    void* device_ptr, * host_ptr;
    cudaSetDeviceFlags(cudaDeviceMapHost);
    cudaHostAlloc((void**)&host_ptr, frameByteSize, cudaHostAllocMapped);
    cudaHostGetDevicePointer((void**)&device_ptr, (void*)host_ptr, 0);
    cv::Mat frame_out(height, width, CV_8UC3, host_ptr);
    cv::cuda::GpuMat d_frame_out(height, width, CV_8UC3, device_ptr);
#else
    /* Unified memory */
    std::cout << "Using unified memory" << std::endl;
    void* unified_ptr;
    cudaMallocManaged(&unified_ptr, frameByteSize);
    cv::Mat frame_out(height, width, CV_8UC3, unified_ptr);
    cv::cuda::GpuMat d_frame_out(height, width, CV_8UC3, unified_ptr);
#endif

..


another example

..

void* m_device_ptr = NULL;
void* m_host_ptr = NULL;

//memory share frame -> cuda_frame
cudaSetDeviceFlags(cudaDeviceMapHost);
size_t frameByteSize = initMat.step[0] * initMat.rows;
cudaHostAlloc((void**)&m_host_ptr, frameByteSize, cudaHostAllocMapped);
cudaHostGetDevicePointer((void**)&m_device_ptr, (void*)m_host_ptr, 0);
cv::Mat m_frame = cv::Mat(initMat.rows, initMat.cols, CV_8UC3, m_host_ptr);
cv::cuda::GpuMat m_cuda_frame = cv::cuda::GpuMat(initMat.rows, initMat.cols, CV_8UC3, m_device_ptr);

//initiation
iframes.copyTo(m_frame);

..



1/12/2023

Opencv Mat <-> UMat , convert Mat to UMat, UMat to Mat

Mat to UMat 

c++

UMat umat;
mat.copyTo(umat);

Python

UMat umat = mat.getUMat( flag );


UMat to Mat

C++

Mat mat = umat.getMat( flag );

Python

mat = cv2.UMat.get(umat)

12/12/2022

opencv c++ resizeWindow example

 Original image size [5120x2188], but you can imshow as 300x300 and resizable.

..

#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"  
#include <iostream>
#include <chrono>
 
#pragma comment(lib, "opencv_highgui453.lib")
#pragma comment(lib, "opencv_imgcodecs453.lib")
#pragma comment(lib, "opencv_core453.lib")
 
int main() {
	//read image
	cv::Mat oImg = cv::imread("Resized_Blackpink_PUBG_210321.jpg");
	cv::namedWindow("oimg", cv::WINDOW_NORMAL);
	cv::resizeWindow("oimg", 300, 300);
	cv::imshow("oimg", oImg);
	cv::waitKey(0);
}

..


Thank you.


7/07/2021

opencv Vector CameraParams save(yaml file store) and load(yaml file restore ) using FileNode, FileNodeIterator


Refer to load & save function.

..

std::vector<cv::detail::CameraParams> params;

bool loadCameraParams(std::string fileName){

cv::FileStorage fs(fileName+".yaml", cv::FileStorage::READ);

if(!fs.isOpened())
return false;

/////
int nObjects= 0;
fs["NC"] >> nObjects;
params.clear();
params.resize(nObjects);
/////
cv::FileNode fn = fs["cameras"];
int id=0;
for (cv::FileNodeIterator it = fn.begin(); it != fn.end(); it++,id++)
{
cv::FileNode item = *it;

cv::Mat K, R, t;
double ppx, ppy, focal, aspect;
item["K"] >> K;
item["R"] >> R;
item["t"] >> t;
item["ppx"] >> ppx;
item["ppy"] >> ppy;
item["focal"] >> focal;
item["aspect"] >> aspect;

mg_params[id].K() = (cv::Mat)K;
mg_params[id].R = R;
mg_params[id].t = t;
mg_params[id].ppx = (double)ppx;
mg_params[id].ppy = (double)ppy;
mg_params[id].focal = (double)focal;
mg_params[id].aspect = (double)aspect;
}

return true;
}

bool saveCameraParams(std::string fileName){

cv::FileStorage fs(fileName+".yaml", cv::FileStorage::WRITE);
cv::detail::CameraParams cameras = mg_params[0];
fs << "NC" << int(params.size());

fs << "cameras" << "[";
for (const auto& param : mg_params)
{
fs << "{";
fs << "K" << param.K();
fs << "R" << param.R;
fs << "t" << param.t;
fs << "ppx" << param.ppx;
fs << "ppy" << param.ppy;
fs << "focal" << param.focal;
fs << "aspect" << param.aspect;
fs << "}";
}
fs << "]";
fs.release();
return true;
}

..


Thank you!!

☕️

1/29/2021

Importerror: libgl.so.1: cannot open shared object file: no such file or directory opencv error

Try this one first:

pip install opencv-python--headless


or 

install follow in case centos or aws linux docker

RUN yum install whatprovides libGL.so.1 -y

install follow in case linux docker

ENV TZ=Europe/Minsk
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get install libgtk2.0-dev -y

 

Thank you, Good luck!

4/16/2020

Python OpenCV Image to byte string for json transfer

code :
import cv2
import base64
import json
import numpy as np

######################################################
#read image
img = cv2.imread('./code_backup/test_img.jpg')
#cv2 to string
image_string = cv2.imencode('.jpg', img)[1]
image_string = base64.b64encode(image_string).decode()
#make string image dict
dict = {'img':image_string}
#save dict to json file
with open('./code_backup/cv2string.json', 'w') as fp:
json.dump(dict, fp, indent=5)
######################################################


######################################################
#read json
response = json.loads(open('./code_backup/cv2string.json', 'r').read())
#get image string
string = response['img']
#convert string to image
jpg_original = base64.b64decode(string)
jpg_as_np = np.frombuffer(jpg_original, dtype=np.uint8)
img = cv2.imdecode(jpg_as_np, flags=1)
#show image
cv2.imshow('show image', img)
cv2.waitKey(0)
######################################################
..

this is input image

this is summarised json file
{ "img": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCAFoAeADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD2BP8Ag34/4JIE4P7I4/8AC617/wCTqsJ/wb4/8EiTx/wyR/5fWv8A/wAnV9nPH2x+FSwx14/t6vc+i+r4f+Q+Mk/4N7P+CQ+P+TRx/wC ........."
}

4/03/2020

Example python code for : Download s3 object as opencv image in memory and upload too

Just see the code
It's not difficult.

...

...
import cv2
import numpy as np
...

def lambda_handler(event, context):
# TODO implement
bucket_name = event['Records'][0]['s3']['bucket']['name']
s3_path = event['Records'][0]['s3']['object']['key']
#download object
obj = s3_client.get_object(Bucket=bucket_name, Key=s3_path)
#obj to cv2
nparr = np.frombuffer(obj['Body'].read(), np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
#simple image processing
reimg = cv2.resize(img, (100,100) )
#cv2 to string
image_string = cv2.imencode('.png', reimg)[1].tostring()
#upload
s3_client.put_object(Bucket='thum-prj-output', Key = s3_path, Body=image_string)
...

...

4/02/2020

PDF to OpenCV as page by page using PyMuPDF library (python example code)

Just see the below example code 😊

pip install PyMuPDF
document : https://pymupdf.readthedocs.io/en/latest/

I think this is better library than pypdf2 🤔
..

import fitz
import numpy as np
import cv2
fname = 'information-10-00248-v2'
doc = fitz.open(fname+'.pdf')

#split pages
for i, page in enumerate(doc.pages()):
print(i)
zoom = 1
mat = fitz.Matrix(zoom, zoom)
pix = page.getPixmap(matrix = mat)
imgData = pix.getImageData("png")
 
#save image from byte
f = open('./save_by_byte_{}_{}.png'.format(fname, i), 'wb')
f.write(imgData)
f.close()
 
#save image from opencv
nparr = np.frombuffer(imgData, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
print(img.shape)
cv2.imwrite('./save_by_opencv_{}_{}.png'.format(fname, i),img)

..