首页 > 解决方案 > Firestore 文档不包含任何数据

问题描述

我有一个 Django 应用程序,可以使用 OCR pytesseract 从他们的盒子中识别药物。获得名称后,我从该网站http://www.dpm.tn/dpm_pharm/medicament/listmedicspec.php抓取数据。最后,在获得具有该药物名称的所有必要数据后,我将其存储在 Firestore 中,但它显示“此文档不包含任何数据”,这是 OCR 的代码:

def ocr_title(im):
    image = cv2.imread(im, 0)
    img = cv2.resize(image, (500, 500))
    img = cv2.GaussianBlur(img, (5, 5), 0)
    img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 21, 4)
    himg, wimg = img.shape
    maxw = 0
    text = ''
    title = pytesseract.image_to_data(img, config='--psm 6 --oem 3')
    for x, b in enumerate(title.splitlines()):
        if x != 0:
            b = b.split()
            if len(b) == 12 and len(b[11]) >= 4:

                if (int(b[8]) > maxw):
                    maxh = int(b[9])
                    maxx = int(b[6])
                    maxy = int(b[7])
                    maxw = int(b[8])
                    text = b[11]
    text = re.sub(r'[^\w]', '', text)
    text = str(text)
    return (text)

这是我在抓取后存储数据的代码(ps:它在 Beautifulsoup 中):

        for i in range(1, len(names)):
            try:
                name = names[i]
                dosage = dosages[i]
                url_product = links[i-1]

                response = requests.post(url_product)

                soup = BeautifulSoup(response.content, 'lxml')

                detail_table = soup.find("table")

                trs = detail_table.find_all('tr')

                detail = soup.find("font", {"color": "#006699"}).text
                form = forms[i]
                data= {
                    "name": name,
                    "dosage": dosage,
                    "form": form,
                    "detail": detail
                }

                med_dosage = dosage.replace(" ", "")
                med_dosage = medecine.lower()+' '+med_dosage.lower()

                med_form = form.replace(" ", "")
                med_form = form.lower()
                #database.child(medecine).child(med_form).child(med_dosage).set(data)
                #db.collection('medecine').collection(medecine).collection(med_form).document(med_dosage).add({
                #    "name": name,
                #    "dosage": dosage,
                #    "form": form,
                #    "detail": detail
                #})
                dbmedecine = db.collection('medecine')

                namemedecine = dbmedecine.document(medecine.lower())

                store = namemedecine.get()

                store.collection(med_form).document(med_dosage).set(data)

                #.collection(med_form).document(med_dosage).set(data)
                print('savee')

            except:
                print("errooooooooooooor")
                print(i)

这是一张来自 Firestore 的照片在此处输入图像描述

标签: djangofirebasegoogle-cloud-firestore

解决方案


推荐阅读