text: import imp
  import torch
  import numpy as np
  from models.experimental import attempt_load
  from utils.general import non_max_suppression, scale_coords, letterbox
  from utils.torch_utils import select_device
  import cv2
  from random import randint
  from processor import change
  from PIL import Image
  from io import BytesIO
  import base64
  import requests
  
  def image_to_base64(img):
      img_str = cv2.imencode('.jpg', img)[1].tostring()  # 将图片编码成流数据，放到内存缓存中，然后转化成string格式
      return base64.b64encode(img_str) # 编码成base64
  
  def ocr(img):
      image=image_to_base64(img)
      request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate"
      params = {"image":image}
      access_token = '24.05905e01e7e5d7a6e96b6a4a11fb36a3.2592000.1649738086.282335-25756395'
      request_url = request_url + "?access_token=" + access_token
      headers = {'content-type': 'application/x-www-form-urlencoded'}
      response = requests.post(request_url, data=params, headers=headers)
      a=response.json()
      print(response.json())
      for i in range(a['words_result_num']):
          print(str(i)+':',end="")
          print(a['words_result'][i]['location'] )
          left=a['words_result'][i]['location']['left']
          top=a['words_result'][i]['location']['top']
          width=a['words_result'][i]['location']['width']
          height=a['words_result'][i]['location']['height']
          x, y = left,top
          w,h=width,height
          ROI = img[y:y+h, x:x+w]
          blur = cv2.GaussianBlur(ROI, (51,51), 10) 
          img[y:y+h, x:x+w] = blur
      return img
  
  class Detector(object):
  
      def __init__(self):
          self.img_size = 640
          self.threshold = 0.4
          self.max_frame = 160
          self.init_model()
  
      def init_model(self):
  
          self.weights = 'weights/best.pt'
          self.device = '0' if torch.cuda.is_available() else 'cpu'
          self.device = select_device(self.device)
          model = attempt_load(self.weights, map_location=self.device)
          model.to(self.device).eval()
          model.float()
          # torch.save(model, 'test.pt')
          self.m = model
          self.names = model.module.names if hasattr(
              model, 'module') else model.names
          self.colors = [
              (randint(0, 255), randint(0, 255), randint(0, 255)) for _ in self.names
          ]
  
      def preprocess(self, img):
  
          img0 = img.copy()
          img = letterbox(img, new_shape=self.img_size)[0]
          img = img[:, :, ::-1].transpose(2, 0, 1)
          img = np.ascontiguousarray(img)
          img = torch.from_numpy(img).to(self.device)
          img = img.float()  # 半精度
          img /= 255.0  # 图像归一化
          if img.ndimension() == 3:
              img = img.unsqueeze(0)
  
          return img0, img
  
      def plot_bboxes(self, image, bboxes, line_thickness=None):
          tl = line_thickness or round(
              0.002 * (image.shape[0] + image.shape[1]) / 2) + 1  # line/font thickness
          for (x1, y1, x2, y2, cls_id, conf) in bboxes:
              color = self.colors[self.names.index(cls_id)]
              c1, c2 = (x1, y1), (x2, y2)
              cv2.rectangle(image, c1, c2, color,
                            thickness=tl, lineType=cv2.LINE_AA)
              tf = max(tl - 1, 1)  # font thickness
              t_size = cv2.getTextSize(
                  cls_id, 0, fontScale=tl / 3, thickness=tf)[0]
              c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
              cv2.rectangle(image, c1, c2, color, -1, cv2.LINE_AA)  # filled
              cv2.putText(image, '{} ID-{:.2f}'.format(cls_id, conf), (c1[0], c1[1] - 2), 0, tl / 3,
                          [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
          return image
      def detect(self, im):
  
          im0, img = self.preprocess(im)
  
          pred = self.m(img, augment=False)[0]
          pred = pred.float()
          pred = non_max_suppression(pred, self.threshold, 0.3)
  
          pred_boxes = []
          image_info = {}
          count = 0
          for det in pred:
              if det is not None and len(det):
                  det[:, :4] = scale_coords(
                      img.shape[2:], det[:, :4], im0.shape).round()
  
                  for *x, conf, cls_id in det:
                      lbl = self.names[int(cls_id)]
                      x1, y1 = int(x[0]), int(x[1])
                      x2, y2 = int(x[2]), int(x[3])
                      pred_boxes.append(
                          (x1, y1, x2, y2, lbl, conf))
                      count += 1
                      key = '{}-{:02}'.format(lbl, count)
                      image_info[key] = ['{}×{}'.format(
                          x2-x1, y2-y1), np.round(float(conf), 3)]
          im = ocr(im)
          im = self.plot_bboxes(im, pred_boxes)
          return im, image_info
  