Skip to content

Instantly share code, notes, and snippets.

im = cv2.imread("/content/amass_images-20200302T050236Z-001/amass_images/190398285_AMI084559_mhp1.JPG")
outputs = predictor(im)
boxes = outputs['instances'].to("cpu").pred_boxes.tensor.numpy()
for box in boxes:
box = list(map(int,box))
crop = im[box[1]:box[3],box[0]:box[2]]
cv2_imshow(crop)
value = pytesseract.image_to_string(crop,lang="chi+chi_tra+eng", config=("--psm 6"))
print(value)
cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_final.pth")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.87 # set the testing threshold for this model
cfg.DATASETS.TEST = ("amass_images_val", )
predictor = DefaultPredictor(cfg)
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_101_C4_3x.yaml"))
cfg.DATASETS.TRAIN = ("amass_images_train",)
cfg.DATASETS.TEST = ("amass_images_val",)
cfg.DATALOADER.NUM_WORKERS = 2
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-Detection/faster_rcnn_R_101_C4_3x.yaml") # Let training initialize from model zoo
cfg.SOLVER.IMS_PER_BATCH = 2
cfg.SOLVER.BASE_LR = 0.00025 # pick a good LR
cfg.SOLVER.MAX_ITER = 5000 # 300 iterations seems good enough for this toy dataset; you may need to train longer for a practical dataset
cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 128 # faster, and good enough for this toy dataset (default: 512)
def common_function(data):
dataset_dict = []
for i in data:
objs = []
record = {}
record["file_name"] =i.get("file_name")
record["image_id"] = i.get("image_id")
record["height"] = i.get("height")
record["width"] = i.get("width")
for ann in i.get("annotations"):
dataset = []
df.annotations[0]
for image in df.images[0]:
data_df = {}
data_df["file_name"] = "/content/amass_images-20200302T050236Z-001/amass_images/"+image.get("file_name")
data_df["width"] = image.get("width")
data_df["height"] = image.get("height")
data_df["image_id"] = image.get("id")
annotations = [annotate for annotate in df.annotations[0] if int(annotate.get("image_id")) == int(image.get("id"))]
data_df['annotations'] = annotations
import detectron2
import pandas as pd
import numpy as np
import cv2, json, os
import random
import pytesseract
from google.colab.patches import cv2_imshow
from detectron2.utils.logger import setup_logger
setup_logger()
from detectron2.data import DatasetCatalog, MetadataCatalog
import random
class InCompleteDeck(Exception):
pass
class Card:
""" This class is used to create a list which contains the tuple where each tuple represent the suit name and card """
def __init__(self):
self.cards = []
self.deckcards()
#Here we see the sub class of StudentModifyManager as StudentModifyManagerFromStudentQuerySet
>>> Student.custom
<django.db.models.manager.StudentModifyManagerFromStudentQuerySet object at 0x7fe74ec3b8d0>
#various other outputs according to our database.
# it return all the students of given age as age is method is defined in StudentQuerySet
>>> Student.custom.age()
<StudentQuerySet [<Student: mayur>, <Student: pranav>, <Student: dhruvesh>, <Student: paras>]>
#Custom Managers to modify Queryset
class MaleManager(Manager):
def get_queryset(self):
return super().get_queryset().filter(gender='male')
class FemaleManager(Manager):
def get_queryset(self):
return super().get_queryset().filter(gender='female')
# Results in shell as:
>>> Student.modify.male().age_greater_or_equal_to(22)
#output is according to our database data
<StudentQuerySet [<Student: binoy>, <Student: mayur>, <Student: pranav>, <Student: dhruvesh>, <Student: anuj>,
<Student: chirag>, <Student: yogi>, <Student: mit>, <Student: dhaval>, <Student: paras>]>