加载OCR模型,进行字符识别
(img, text_recs, adjust=False)
| 38 | return imgOut |
| 39 | |
| 40 | def charRec(img, text_recs, adjust=False): |
| 41 | """ |
| 42 | 加载OCR模型,进行字符识别 |
| 43 | """ |
| 44 | results = {} |
| 45 | xDim, yDim = img.shape[1], img.shape[0] |
| 46 | |
| 47 | for index, rec in enumerate(text_recs): |
| 48 | xlength = int((rec[6] - rec[0]) * 0.1) |
| 49 | ylength = int((rec[7] - rec[1]) * 0.2) |
| 50 | if adjust: |
| 51 | pt1 = (max(1, rec[0] - xlength), max(1, rec[1] - ylength)) |
| 52 | pt2 = (rec[2], rec[3]) |
| 53 | pt3 = (min(rec[6] + xlength, xDim - 2), min(yDim - 2, rec[7] + ylength)) |
| 54 | pt4 = (rec[4], rec[5]) |
| 55 | else: |
| 56 | pt1 = (max(1, rec[0]), max(1, rec[1])) |
| 57 | pt2 = (rec[2], rec[3]) |
| 58 | pt3 = (min(rec[6], xDim - 2), min(yDim - 2, rec[7])) |
| 59 | pt4 = (rec[4], rec[5]) |
| 60 | |
| 61 | degree = degrees(atan2(pt2[1] - pt1[1], pt2[0] - pt1[0])) # 图像倾斜角度 |
| 62 | |
| 63 | partImg = dumpRotateImage(img, degree, pt1, pt2, pt3, pt4) |
| 64 | |
| 65 | if partImg.shape[0] < 1 or partImg.shape[1] < 1 or partImg.shape[0] > partImg.shape[1]: # 过滤异常图片 |
| 66 | continue |
| 67 | |
| 68 | image = Image.fromarray(partImg).convert('L') |
| 69 | text = keras_densenet(image) |
| 70 | |
| 71 | if len(text) > 0: |
| 72 | results[index] = [rec] |
| 73 | results[index].append(text) # 识别文字 |
| 74 | |
| 75 | return results |
| 76 | |
| 77 | def model(img, adjust=False): |
| 78 | """ |
no test coverage detected