Skip to content

Instantly share code, notes, and snippets.

@sarsamurmu
Last active January 28, 2025 09:03
Show Gist options
  • Save sarsamurmu/4d7762be07c548a1736c60bd98654fed to your computer and use it in GitHub Desktop.
Save sarsamurmu/4d7762be07c548a1736c60bd98654fed to your computer and use it in GitHub Desktop.
New Image Occlusion with Auto Occlude server

Watch the video - https://youtu.be/3FSvEqOEAl8

Setting up server

  • First install python in your system and make sure that these commands (run in terminal) give no error. The recommended version is 3.12.7, newer versions may cause errors with the dependencies
python --version
pip --version
  • Then save the server.py file to a directory and run these commands in terminal to install dependencies
pip install paddleocr paddlepaddle numpy setuptools

Setting up addon in Anki

  • Uninstall the "Image Occlusion Enhanced" from Anki
  • Download this zip
  • Extract it and copy this directory image-occlusion-sm/src/image_occlusion_sm, you will paste it later
  • Open Anki, go to Tools > Add-ons > View files
  • File explorer will pop up, paste the copied directory in this directory
  • Restart Anki

Starting the server

  • Open the directory where you saved server.py
  • Open terminal window in the same directory
  • Run this command
python server.py
  • Awesome!! Now you can use the "Auto occlude" feature!

NOTE: You will have to start the server everytime you restart your computer

from http.server import BaseHTTPRequestHandler, HTTPServer
from cgi import parse_header, parse_multipart
from urllib.parse import parse_qs
from http.server import BaseHTTPRequestHandler
import json
import cv2
import numpy as np
from paddleocr import PaddleOCR
ocr = PaddleOCR(use_angle_cls=True, lang='en', use_gpu=True)
class npEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.int32):
return int(obj)
return json.JSONEncoder.default(self, obj)
class handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
message = "Hello, World! Here is a GET response"
self.wfile.write(bytes(message, "utf8"))
def parse_POST(self):
ctype, pdict = parse_header(self.headers['content-type'])
if ctype == 'multipart/form-data':
postvars = parse_multipart(self.rfile, pdict)
elif ctype == 'application/x-www-form-urlencoded':
length = int(self.headers['content-length'])
postvars = parse_qs(
self.rfile.read(length),
keep_blank_values=1)
else:
postvars = {}
return postvars
def do_POST(self):
postvars = self.parse_POST()
print(postvars)
img_path = postvars[b"image"][0].decode()
result = ocr.ocr(img_path, rec=True, det=True)
height, width, c = cv2.imread(img_path).shape
self.send_response(200)
self.send_header('Content-type','application/json')
self.end_headers()
pos = []
for det in result[0]:
pos.append([det[0][0], det[0][2]])
# pos.append([det[0], det[2]])
message = '{"rects":' f'{json.dumps(pos, cls=npEncoder)}' ',"dim":' f'[{width}, {height}]' '}'
# print(message)
self.wfile.write(bytes(message, "utf8"))
with HTTPServer(('', 27030), handler) as server:
server.serve_forever()
# 1.21.1
# 1.23.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment