Export Python3 version OpenCV KeyPoint to a file

When I tried to write KeyPoint to a file using the Pickle module, I got an error. It is a memo of the countermeasure at that time. If you search for it, it will come out quite quickly, so there may not be much demand, but for the time being.

environment

Reference URL

http://stackoverflow.com/questions/26501193/opencv-python-find-a-code-for-writing-keypoins-to-a-file Almost the same as the answer here

Failure example

out_kp_and_des.py


import cv2
import pickle


img = cv2.imread('XXXX.jpg')
detector = cv2.AKAZE_create()
#keypoints and descriptors
kp, des = detector.detectAndCompute(img, None)

with open('XXXX.pickle', mode='wb') as f:
    pickle.dump((kp, des), f)

If you try to Pickle KeyPoint as it is, you will get an error like `` `PicklingError: Can't pickle <class'cv2.KeyPoint'>: it's not the same object as cv2.KeyPoint```.

solution

out_kp_and_des.py


import cv2
import pickle


img = cv2.imread('XXXX.jpg')
detector = cv2.AKAZE_create()
kp, des = detector.detectAndCompute(img, None)

index = []
for p in kp:
    temp = (p.pt, p.size, p.angle, p.response, p.octave, p.class_id)
    index.append(temp)
with open('XXXX.pickle', mode='wb') as f:
    pickle.dump((index, des), f)

Take out the KeyPoint attribute and put it in the list to make it feel like Pickle. The file reading looks like the following.

load_kp_and_des.py


import cv2
import pickle


with open('XXXX.pickle', mode='rb') as f:
    index, des = pickle.load(f)
kp = []
for p in index:
    temp = cv2.KeyPoint(x=p[0][0], y=p[0][1], _size=p[1], _angle=p[2],
                        _response=p[3], _octave=p[4], _class_id=p[5])
    kp.append(temp)

Recommended Posts

Export Python3 version OpenCV KeyPoint to a file
How to create a JSON file in Python
Introduction to OpenCV (python)-(2)
I want to write to a file with Python
Parse a JSON string written to a file in Python
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
Python script to create a JSON file from a CSV file
A road to intermediate Python
Introduction to Python (Python version APG4b)
How to change Python version
Export a gzip-compressed text file
[python] Change the image file name to a serial number
How to run a Python file at a Windows 10 command prompt
A Python script that saves a clipboard (GTK) image to a file.
Change the standard output destination to a file in Python
How to import a file anywhere you like in Python
[Python] How to output a pandas table to an excel file
How to write a Python class
Output to csv file with Python
Write standard output to a file
How to get the Python version
Create a binary file in Python
5 Ways to Create a Python Chatbot
Introduction to image analysis opencv python
Python version to get unused ports
How to create a config file
[AWS SAM] Introduction to Python version
File upload to Azure Storage (Python)
I tried to convert a Python file to EXE (Recursion error supported)
Process Splunk execution results using Python and save to a file
Create a shell script to run the python file multiple times
How to make a surveillance camera (Security Camera) with Opencv and Python
I tried to display the video playback time (OpenCV: Python version)
If you want to assign csv export to a variable in python
Python vba to create a date string for creating a file name
I made a library that adds docstring to a Python stub file.
[ROS2] How to play a bag file with python format launch
Try adding a wall to your IFC file with IfcOpenShell python
[Python] How to convert db file to csv
Creating a simple PowerPoint file with Python
Create a shortcut to run a Python file in VScode on your terminal
[Python] How to make a class iterable
[Python] How to convert a 2D list to a 1D list
How to convert Python to an exe file
A super introduction to Python bit operations
[Python] Convert csv file delimiters to tab delimiters
Send a message from Python to Slack
Convert psd file to png in Python
I want to build a Python environment
[Python] How to invert a character string
How to get a stacktrace in python
[Python] How to read a csv file (read_csv method of pandas module)
Read Python csv and export to txt
Create a deb file from a python package
[GPS] Create a kml file in Python
Tips for Python beginners to use Scikit-image examples for themselves 3 Write to a file
Script to create a Mac dictionary file
Build a Python + OpenCV environment on Cloud9
A way to understand Python duck typing
I made a program to check the size of a file in Python