修正圖像初始化錯誤
This commit is contained in:
		
							
								
								
									
										35
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								README.md
									
									
									
									
									
								
							@@ -161,3 +161,38 @@ GET    | /api/images?sort=choice        | 根据精选集推荐(指定精选集I
 | 
			
		||||
 | 
			
		||||
* 注意, 筛选规则为多条件取交集, 单条件的复数取并集
 | 
			
		||||
* 权重强化属于排序规则而非过滤规则
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 獲取任務列表(標準查询)
 | 
			
		||||
 | 
			
		||||
GET /api/tasks
 | 
			
		||||
```javascript
 | 
			
		||||
{
 | 
			
		||||
    page: 1,             // 当前页码
 | 
			
		||||
    pageSize: 20,        // 分页数
 | 
			
		||||
    next: true,          // 是否存在下一页
 | 
			
		||||
    list: [{
 | 
			
		||||
        id: 'xxxxxx',    // 任務ID
 | 
			
		||||
        type: '',        // 任務類型(推理, 訓練)
 | 
			
		||||
        data: {},        // 任務執行數據
 | 
			
		||||
        create_time: ''  // 任務創建時間
 | 
			
		||||
        update_time: ''  // 任務更新時間
 | 
			
		||||
    }],
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Websocket /api/tasks/{task_id}
 | 
			
		||||
```javascript
 | 
			
		||||
{
 | 
			
		||||
    // 狀態
 | 
			
		||||
    // 進度
 | 
			
		||||
    // 結果
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
* 通過websocket監聽任務狀態變化
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 模型(我的|共享|熱門|查詢)
 | 
			
		||||
### 圖片(我的|共享|熱門|查詢)
 | 
			
		||||
### 標籤
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										12
									
								
								bin/main.go
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								bin/main.go
									
									
									
									
									
								
							@@ -543,7 +543,11 @@ func main() {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		var img models.Image
 | 
			
		||||
		img.Init(content)
 | 
			
		||||
		if err := img.Init(content); err != nil {
 | 
			
		||||
			log.Println("初始化图片失败", format, err)
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusBadRequest)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		data, err := img.ToWebP(width, height, fit)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("转换图片失败", err)
 | 
			
		||||
@@ -575,7 +579,11 @@ func main() {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		var img models.Image
 | 
			
		||||
		img.Init(content)
 | 
			
		||||
		if err := img.Init(content); err != nil {
 | 
			
		||||
			log.Println("初始化图片失败", version, format, err)
 | 
			
		||||
			w.WriteHeader(http.StatusNotFound)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		data, err := img.ToWebP(width, height, fit)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("转换图片失败", err)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										68
									
								
								bin/ocr.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								bin/ocr.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,68 @@
 | 
			
		||||
# python 實現圖片文字辨識(ocr)
 | 
			
		||||
 | 
			
		||||
import pytesseract
 | 
			
		||||
from PIL import Image
 | 
			
		||||
 | 
			
		||||
# 設置tesseract執行文件的路徑(linux)
 | 
			
		||||
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
 | 
			
		||||
 | 
			
		||||
# 設置提取中文和英文
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# 讀取圖片
 | 
			
		||||
image = Image.open('data/tt.jpeg')
 | 
			
		||||
 | 
			
		||||
# 圖片轉為灰度圖
 | 
			
		||||
image = image.convert('L')
 | 
			
		||||
 | 
			
		||||
# 圖片二值化
 | 
			
		||||
threshold = 127
 | 
			
		||||
table = []
 | 
			
		||||
for i in range(256):
 | 
			
		||||
    if i < threshold:
 | 
			
		||||
        table.append(0)
 | 
			
		||||
    else:
 | 
			
		||||
        table.append(1)
 | 
			
		||||
image = image.point(table, '1')
 | 
			
		||||
 | 
			
		||||
# 圖片轉為字符串
 | 
			
		||||
#text = pytesseract.image_to_string(image, lang='chi_sim+eng')
 | 
			
		||||
#print(text)
 | 
			
		||||
#
 | 
			
		||||
#data = pytesseract.image_to_boxes(image, lang='chi_sim+eng')
 | 
			
		||||
#print(data)
 | 
			
		||||
 | 
			
		||||
# 座標(全數據)
 | 
			
		||||
#data = pytesseract.image_to_data(image, lang='chi_sim+eng')
 | 
			
		||||
#print(data)
 | 
			
		||||
 | 
			
		||||
data = pytesseract.image_to_alto_xml(image, lang='chi_sim+eng')
 | 
			
		||||
#print(data)
 | 
			
		||||
 | 
			
		||||
## 打印格式化的XML
 | 
			
		||||
#from xml.dom.minidom import parseString
 | 
			
		||||
#dom = parseString(data)
 | 
			
		||||
#print(dom.toprettyxml())
 | 
			
		||||
 | 
			
		||||
# 打印格式化的JSON
 | 
			
		||||
import json
 | 
			
		||||
from xmljson import badgerfish as bf
 | 
			
		||||
from xml.etree.ElementTree import fromstring
 | 
			
		||||
 | 
			
		||||
# 過濾掉@CONTENT爲空的數據和爲數字的數據
 | 
			
		||||
def filter_data(data):
 | 
			
		||||
    if isinstance(data, dict):
 | 
			
		||||
        if '@CONTENT' in data:
 | 
			
		||||
            if data['@CONTENT'] == '' or data['@CONTENT'].isdigit():
 | 
			
		||||
                return None
 | 
			
		||||
        for k, v in data.items():
 | 
			
		||||
            if isinstance(v, dict):
 | 
			
		||||
                data[k] = filter_data(v)
 | 
			
		||||
            elif isinstance(v, list):
 | 
			
		||||
                data[k] = [filter_data(i) for i in v]
 | 
			
		||||
    return data
 | 
			
		||||
 | 
			
		||||
xml = fromstring(data)
 | 
			
		||||
json = json.dumps(bf.data(xml), indent=4, ensure_ascii=False)
 | 
			
		||||
print(json)
 | 
			
		||||
 | 
			
		||||
@@ -2,6 +2,7 @@ package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"image"
 | 
			
		||||
	"io"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
@@ -20,13 +21,15 @@ type Image struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 初始化图片
 | 
			
		||||
func (img *Image) Init(content string) {
 | 
			
		||||
func (img *Image) Init(content string) (err error) {
 | 
			
		||||
	var body io.ReadCloser
 | 
			
		||||
 | 
			
		||||
	if len(regexp.MustCompile(`image.gameuiux.cn`).FindStringSubmatch(content)) > 0 {
 | 
			
		||||
		key := regexp.MustCompile(`^https?://image.gameuiux.cn/`).ReplaceAllString(content, "")
 | 
			
		||||
 | 
			
		||||
		// 从OSS中读取图片
 | 
			
		||||
		bucket := GetBucket("gameui-image2")
 | 
			
		||||
		body, err := bucket.GetObject(key)
 | 
			
		||||
		body, err = bucket.GetObject(key)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("读取图片失败", err)
 | 
			
		||||
			return
 | 
			
		||||
@@ -52,8 +55,9 @@ func (img *Image) Init(content string) {
 | 
			
		||||
		}
 | 
			
		||||
		return
 | 
			
		||||
	} else {
 | 
			
		||||
		var resp *http.Response
 | 
			
		||||
		log.Println("直接从网络下载图片:", content)
 | 
			
		||||
		resp, err := http.Get(content)
 | 
			
		||||
		resp, err = http.Get(content)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("下载图片失败", err)
 | 
			
		||||
			return
 | 
			
		||||
@@ -78,6 +82,7 @@ func (img *Image) Init(content string) {
 | 
			
		||||
			log.Println("解码图像失败", err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user