Exemple #1
0
class DataExtraction(unittest.TestCase):
    def setUp(self):
        warnings.simplefilter("ignore", category=ResourceWarning)

        self.sypht_client = SyphtClient(os.environ["CLIENT_ID"],
                                        os.environ["CLIENT_SECRET"])

    def test_with_wrong_fieldset(self):
        with self.assertRaises(Exception) as context:
            with open("tests/sample_invoice.pdf", "rb") as f:
                response = self.sypht_client.upload(
                    f,
                    [
                        "sypht.incorrect",
                    ],
                )
                self.assertIn(
                    "does not have permission to use fieldSet sypht.incorrect",
                    response["error"],
                )

        self.assertTrue(
            "Request failed with status code" in str(context.exception))

    def test_data_extraction_1(self):
        with open("tests/sample_invoice.pdf", "rb") as f:
            fid = self.sypht_client.upload(f, ["invoices"])
            self.assertTrue(validate_uuid4(fid))

        results = self.sypht_client.fetch_results(fid)

        self.assertTrue(isinstance(results, dict))
        self.assertIn("invoice.dueDate", results)
        self.assertIn("invoice.total", results)
        self.assertIn("invoice.amountPaid", results)
        self.assertIn("invoice.amountDue", results)

    def test_data_extraction_2(self):
        with open("tests/sample_invoice.pdf", "rb") as f:
            fid = self.sypht_client.upload(
                f, products=["sypht.invoice", "sypht.bank"])
            self.assertTrue(validate_uuid4(fid))

        results = self.sypht_client.fetch_results(fid)

        self.assertTrue(isinstance(results, dict))
        self.assertIn("invoice.dueDate", results)
        self.assertIn("invoice.total", results)
        self.assertIn("invoice.amountPaid", results)
        self.assertIn("invoice.amountDue", results)
        self.assertIn("bank.accountNo", results)
        self.assertIn("bank.bsb", results)

    def test_parent_doc_id(self):
        parent_doc_id = uuid4()
        with open("tests/sample_invoice.pdf", "rb") as f:
            fid = self.sypht_client.upload(f, ["invoices"],
                                           parent_doc_id=parent_doc_id)
            self.assertTrue(validate_uuid4(fid))
class DataExtraction(unittest.TestCase):
    def setUp(self):
        if not six.PY2:
            warnings.simplefilter('ignore', category=ResourceWarning)

        self.sypht_client = SyphtClient(os.environ['CLIENT_ID'],
                                        os.environ['CLIENT_SECRET'])

    def test_with_wrong_fieldset(self):
        with self.assertRaises(Exception) as context:
            with open('tests/sample_invoice.pdf', 'rb') as f:
                response = self.sypht_client.upload(f,
                                                    fieldsets=[
                                                        'sypht.incorrect',
                                                    ])
                self.assertIn(
                    'does not have permission to use fieldSet sypht.incorrect',
                    reponse['error'])

        self.assertTrue(
            'Request failed with status code' in str(context.exception))

    def test_data_extraction_1(self):
        with open('tests/sample_invoice.pdf', 'rb') as f:
            fid = self.sypht_client.upload(f, fieldsets=Fieldset.INVOICE)
            self.assertTrue(validate_uuid4(fid))

        results = self.sypht_client.fetch_results(fid)

        self.assertTrue(isinstance(results, dict))
        self.assertIn('invoice.dueDate', results)
        self.assertIn('invoice.total', results)
        self.assertIn('invoice.amountPaid', results)
        self.assertIn('invoice.amountDue', results)

    def test_data_extraction_2(self):
        with open('tests/sample_invoice.pdf', 'rb') as f:
            fid = self.sypht_client.upload(f,
                                           fieldsets=[
                                               Fieldset.INVOICE,
                                               Fieldset.BANK,
                                           ])
            self.assertTrue(validate_uuid4(fid))

        results = self.sypht_client.fetch_results(fid)

        self.assertTrue(isinstance(results, dict))
        self.assertIn('invoice.dueDate', results)
        self.assertIn('invoice.total', results)
        self.assertIn('invoice.amountPaid', results)
        self.assertIn('invoice.amountDue', results)
        self.assertIn('bank.accountNo', results)
        self.assertIn('bank.bsb', results)
    def __call__(self):
        sypht = SyphtClient()

        print("Uploading: ", self.path, "...")
        with open(self.path, "rb") as f:
            doc_id = sypht.upload(f, self.products)

        print("Processing: ", doc_id, "...")
        print(json.dumps(sypht.fetch_results(doc_id), indent=2))
    def __call__(self):
        sypht = SyphtClient()

        print('Uploading: ', self.path, '...')
        with open(self.path, 'rb') as f:
            doc_id = sypht.upload(f, self.fieldsets)

        print('Processing: ', doc_id, '...')
        print(json.dumps(sypht.fetch_results(doc_id), indent=2))
import pandas as pd

#give path of receipts folder to extract there dates
folder = r"C:\Users\..\Downloads\Desktop\syphtclient\Receipts"
dates = []
filenames = []
for filename in os.listdir(folder):
    print(filename)
    #jpgfile = Image.open(os.path.join(folder, filename))
    with open(os.path.join(folder, filename), 'rb') as f:

        fid = scc.upload(
            f, fieldsets=["document"]
        )  ###obtaining results by uploading image on sypht through sypht api key
        f.close()
    ab = scc.fetch_results(fid)  ###fetching results
    print(scc.fetch_results(fid))
    filenames.append(filename)
    if not ab:  ###check the dictionary is empty or not
        #print('none')
        dates.append('none')
    else:
        dates.append(ab['document.date'])

###store the dates and filenames respectively into a csv file
result = pd.DataFrame({'filenames': filenames, 'dates': dates})
#result2.to_csv('result2.csv')

#result1=pd.read_csv('result1.csv')
#result=pd.concat([result1,result2],ignore_index=True)
result.to_csv('result.csv')
# get the ratio between average shadow pixels and average lit pixels
ratio_as_al = average_ld / average_le

# added these difference
for i in range(y_cb_cr_img.shape[0]):
    for j in range(y_cb_cr_img.shape[1]):
        if erosion[i, j, 0] == 255 and erosion[i, j,
                                               1] == 255 and erosion[i, j,
                                                                     2] == 255:

            y_cb_cr_img[i, j] = [
                y_cb_cr_img[i, j, 0] + i_diff,
                y_cb_cr_img[i, j, 1] + ratio_as_al,
                y_cb_cr_img[i, j, 2] + ratio_as_al
            ]

# covert the YCbCr image to the BGR image
final_image = cv2.cvtColor(y_cb_cr_img, cv2.COLOR_YCR_CB2BGR)
cv2.imwrite('images/new-{}'.format(image_filename), final_image)
from sypht.client import SyphtClient, Fieldset

scc = SyphtClient('JNSJgJF3SEltPf11DZLtdPE9SFPdrmlh',
                  '......................................................')
with open('images/new-{}'.format(image_filename), 'rb') as f:
    fid = scc.upload(f, fieldsets=["document"])
print(scc.fetch_results(fid))
cv2.imshow("im1", or_img)
cv2.imshow("im2", final_image)
cv2.waitKey(0)
cv2.destroyAllWindows()