Browse Source

fixes to image tables

main
westernmeadow 1 year ago
parent
commit
7687fc283c
4 changed files with 47 additions and 41 deletions
  1. +23
    -23
      Forums/DB_Connection/db_connection.py
  2. +3
    -0
      Forums/Utilities/utilities.py
  3. +18
    -18
      MarketPlaces/DB_Connection/db_connection.py
  4. +3
    -0
      MarketPlaces/Utilities/utilities.py

+ 23
- 23
Forums/DB_Connection/db_connection.py View File

@ -118,9 +118,9 @@ def verifyImage(cur, base64Image):
try: try:
cur.execute("lock table market_images IN ACCESS EXCLUSIVE MODE")
cur.execute("lock table forum_images IN ACCESS EXCLUSIVE MODE")
cur.execute("select image_id from market_images where hash_image = %(hashImage)s limit 1",
cur.execute("select image_id from forum_images where hash_image = %(hashImage)s limit 1",
{'hashImage': generate_image_hash(base64Image)}) {'hashImage': generate_image_hash(base64Image)})
recset = cur.fetchall() recset = cur.fetchall()
@ -273,7 +273,7 @@ def getLastImage(cur):
try: try:
cur.execute("select image_id from market_images order by image_id desc limit 1")
cur.execute("select image_id from forum_images order by image_id desc limit 1")
recset = cur.fetchall() recset = cur.fetchall()
@ -406,7 +406,7 @@ def create_author(cur, row, forumId):
"signature_user, image_user, dateinserted_user) Values (%s, %s, %s, %s, %s, %s, %s, %s, %s)" "signature_user, image_user, dateinserted_user) Values (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
recset = [userId, forumId, recset = [userId, forumId,
row[2], 'Nan', 'Nan', 'Nan', 'Nan', 'Nan', #telling the create_user function to not track changes here
row[2], 'Nan', 'Nan', 'Nan', 'Nan', None, #telling the create_user function to not track changes here
row[8]] row[8]]
cur.execute(sql, recset) cur.execute(sql, recset)
@ -451,7 +451,7 @@ def create_user(cur, row, forumId, index):
recset = cur.fetchall() recset = cur.fetchall()
imageId = recset[0]['image_user'] imageId = recset[0]['image_user']
if not is_same_image(row[9][index], recset[0]['image_user'])):
if not is_same_image(cur, row[9][index], recset[0]['image_user']):
imageId = create_image(cur, row[9][index], row[8]) imageId = create_image(cur, row[9][index], row[8])
if (str(recset[0]['status_user']) != str(row[11][index] if row[11][index] != '-1' else None) or if (str(recset[0]['status_user']) != str(row[11][index] if row[11][index] != '-1' else None) or
@ -464,7 +464,7 @@ def create_user(cur, row, forumId, index):
str(recset[0]['reputation_user']) != 'Nan' or str(recset[0]['reputation_user']) != 'Nan' or
str(recset[0]['interest_user']) != 'Nan' or str(recset[0]['interest_user']) != 'Nan' or
str(recset[0]['signature_user']) != 'Nan' or str(recset[0]['signature_user']) != 'Nan' or
str(recset[0]['image_user']) != 'Nan'):
str(recset[0]['image_user']) != str(None)):
userVersionId = int(getLastUserVersion(cur, userId) + 1) userVersionId = int(getLastUserVersion(cur, userId) + 1)
@ -537,7 +537,7 @@ def create_posts(cur, row, forumId, topicId):
recset = cur.fetchall() recset = cur.fetchall()
imageId = recset[0]['image_post'] imageId = recset[0]['image_post']
if not is_same_image(row[18][i], recset[0]['image_post'])):
if not is_same_image(cur, row[18][i], recset[0]['image_post']):
imageId = create_image(cur, row[18][i], row[8]) imageId = create_image(cur, row[18][i], row[8])
if (str(recset[0]['content_post']) != str(row[15][i]) or if (str(recset[0]['content_post']) != str(row[15][i]) or
@ -566,23 +566,23 @@ def create_posts(cur, row, forumId, topicId):
'dateinserted_post': row[8], 'dateinserted_post': row[8],
'postId': postId}) 'postId': postId})
def is_same_image(newBase64Image, oldImageId):
def is_same_image(cur, newBase64Image, oldImageId):
if newBase64Image == "-1" and oldImageId == "-1":
if newBase64Image == "-1" and str(oldImageId) is str(None):
return True return True
if newBase64Image == "-1": # and oldImageId != "-1"
if newBase64Image == "-1": # and str(oldImageId) != str(None)
return False return False
if oldImageId == "-1": # and newBase64Image != "-1"
if str(oldImageId) == str(None): # and newBase64Image != "-1"
return False return False
sql = "select * from market_images where image_id = %(imageId)s"
sql = "select * from forum_images where image_id = %(imageId)s"
cur.execute(sql, {'imageId': oldImageId}) cur.execute(sql, {'imageId': oldImageId})
recset = cur.fetchall() recset = cur.fetchall()
decImage = decode_decrypt_image_in_base64(base64Image)
decImage = decode_decrypt_image_in_base64(newBase64Image)
hash1 = generate_image_hash(decImage) hash1 = generate_image_hash(decImage)
hash2 = recset[0]['hash_image'] hash2 = recset[0]['hash_image']
@ -590,19 +590,21 @@ def is_same_image(newBase64Image, oldImageId):
if hash1 == hash2: if hash1 == hash2:
return True return True
features1 = json.loads(extract_hidden_layer_output(dec_string))
features1 = json.loads(extract_hidden_layer_output(decImage))
features2 = json.loads(recset[0]['resnet50_image']) features2 = json.loads(recset[0]['resnet50_image'])
similarity = 1 - distance.cosine(features1, features2) similarity = 1 - distance.cosine(features1, features2)
return similarity < 0.8
return similarity >= 0.8
def create_image(cur, base64Image, date): def create_image(cur, base64Image, date):
if base64Image == "-1": if base64Image == "-1":
return None return None
imageId = verifyImage(base64Image)
decImage = decode_decrypt_image_in_base64(base64Image)
imageId = verifyImage(cur, decImage)
if not imageId: if not imageId:
imageId = int(getLastImage(cur) + 1) imageId = int(getLastImage(cur) + 1)
@ -611,15 +613,13 @@ def create_image(cur, base64Image, date):
newImage = False newImage = False
if newImage: if newImage:
decImage = decode_decrypt_image_in_base64(base64Image)
hashImage = generate_image_hash(decImage) hashImage = generate_image_hash(decImage)
hloFeatures = extract_hidden_layer_output(decImage) hloFeatures = extract_hidden_layer_output(decImage)
keypoints, descriptors = extract_keypoints(decImage) keypoints, descriptors = extract_keypoints(decImage)
sql = "Insert into market_images(image_id, hash_image, base64_image, " \
"resnet50_image, siftkey_image, siftdesc_image, dateinserted_image) " \
sql = "Insert into forum_images(image_id, hash_image, base64_image, " \
"resnet50_image, siftkey_image, siftdesc_image, dateinserted_image) " \
"Values (%s, %s, %s, %s, %s, %s, %s)" "Values (%s, %s, %s, %s, %s, %s, %s)"
recset = [imageId, recset = [imageId,
@ -662,11 +662,11 @@ def create_database(cur, con):
sql = "create table forum_images(image_id integer not null, hash_image character varying(64) not null, base64_image character varying(10000000) not null, " \ sql = "create table forum_images(image_id integer not null, hash_image character varying(64) not null, base64_image character varying(10000000) not null, " \
"resnet50_image character varying(1000000) null, siftkey_image character varying(1000000) null, siftdesc_image character varying(1000000) null, " \ "resnet50_image character varying(1000000) null, siftkey_image character varying(1000000) null, siftdesc_image character varying(1000000) null, " \
"dateinserted_item timestamp(6) with time zone not null, " \
"constraint items_pk primary key (image_id))"
"dateinserted_image timestamp(6) with time zone not null, " \
"constraint forum_images_pk primary key (image_id))"
cur.execute(sql) cur.execute(sql)
sql = "create unique index unique_image ON forum_images USING btree (hash_image ASC NULLS LAST)"
sql = "create unique index unique_forum_image ON forum_images USING btree (hash_image ASC NULLS LAST)"
cur.execute(sql) cur.execute(sql)
sql = "create table forums (forum_id integer NOT NULL, name_forum character varying(255) NOT NULL, url_forum " \ sql = "create table forums (forum_id integer NOT NULL, name_forum character varying(255) NOT NULL, url_forum " \


+ 3
- 0
Forums/Utilities/utilities.py View File

@ -89,6 +89,9 @@ def extract_keypoints(image_string):
keypoints, descriptors = sift.detectAndCompute(img, None) keypoints, descriptors = sift.detectAndCompute(img, None)
if len(keypoints) == 0:
return None, None
return json.dumps(wrap_keypoints(keypoints)), json.dumps(descriptors.tolist()) return json.dumps(wrap_keypoints(keypoints)), json.dumps(descriptors.tolist())


+ 18
- 18
MarketPlaces/DB_Connection/db_connection.py View File

@ -273,7 +273,7 @@ def create_vendor(cur, row, marketId):
recset = cur.fetchall() recset = cur.fetchall()
imageId = recset[0]['image_vendor'] imageId = recset[0]['image_vendor']
if not is_same_image(row[21], recset[0]['image_vendor'])):
if not is_same_image(cur, row[21], recset[0]['image_vendor']):
imageId = create_image(cur, row[21], row[23]) imageId = create_image(cur, row[21], row[23])
if (str(recset[0]['rating_vendor']) != str(row[2] if row[2] != '-1' else None) or # there was a change in the vendor information if (str(recset[0]['rating_vendor']) != str(row[2] if row[2] != '-1' else None) or # there was a change in the vendor information
@ -362,7 +362,7 @@ def create_items(cur, row, marketId, vendorId):
recset = cur.fetchall() recset = cur.fetchall()
imageId = recset[0]['image_item'] imageId = recset[0]['image_item']
if not is_same_image(row[20], recset[0]['image_item'])):
if not is_same_image(cur, row[20], recset[0]['image_item']):
imageId = create_image(cur, row[20], row[23]) imageId = create_image(cur, row[20], row[23])
if (str(recset[0]['vendor_id']) != str(vendorId) or if (str(recset[0]['vendor_id']) != str(vendorId) or
@ -389,7 +389,7 @@ def create_items(cur, row, marketId, vendorId):
sql = "Insert into items_history (item_id, version_item, market_id, vendor_id, name_item, description_item, cve_item, ms_item, category_item, " \ sql = "Insert into items_history (item_id, version_item, market_id, vendor_id, name_item, description_item, cve_item, ms_item, category_item, " \
"views_item, reviews_item, rating_item, dateadded_item, btc_item, usd_item, euro_item, quantitysold_item, " \ "views_item, reviews_item, rating_item, dateadded_item, btc_item, usd_item, euro_item, quantitysold_item, " \
"quantityleft_item, shippedfrom_item, shippedto_item, lastseen_item, image_item, href_item, dateinserted_item, " \
"quantityleft_item, shippedfrom_item, shippedto_item, image_item, href_item, lastseen_item, dateinserted_item, " \
"classification_item) Values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, " \ "classification_item) Values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, " \
"%s, %s, %s, %s, %s, %s)" "%s, %s, %s, %s, %s, %s)"
@ -425,7 +425,7 @@ def create_items(cur, row, marketId, vendorId):
"rating_item = %(rating_item)s, dateadded_item = %(dateadded_item)s, btc_item = %(btc_item)s, " \ "rating_item = %(rating_item)s, dateadded_item = %(dateadded_item)s, btc_item = %(btc_item)s, " \
"usd_item = %(usd_item)s, euro_item = %(euro_item)s, quantitysold_item = %(quantitysold_item)s, " \ "usd_item = %(usd_item)s, euro_item = %(euro_item)s, quantitysold_item = %(quantitysold_item)s, " \
"quantityleft_item = %(quantityleft_item)s, shippedfrom_item = %(shippedfrom_item)s, shippedto_item = %(shippedto_item)s, " \ "quantityleft_item = %(quantityleft_item)s, shippedfrom_item = %(shippedfrom_item)s, shippedto_item = %(shippedto_item)s, " \
"image_item = %(image_item)s, lastseen_item = %(lastseen_item)s, dateinserted_item = %(dateinserted_item)s." \
"image_item = %(image_item)s, lastseen_item = %(lastseen_item)s, dateinserted_item = %(dateinserted_item)s, " \
"classification_item = %(classification_item)s where item_id = %(itemId)s" "classification_item = %(classification_item)s where item_id = %(itemId)s"
cur.execute(sql, {'vendor_id': vendorId, cur.execute(sql, {'vendor_id': vendorId,
@ -460,15 +460,15 @@ def create_items(cur, row, marketId, vendorId):
return itemId return itemId
def is_same_image(newBase64Image, oldImageId):
def is_same_image(cur, newBase64Image, oldImageId):
if newBase64Image == "-1" and oldImageId == "-1":
if newBase64Image == "-1" and str(oldImageId) == str(None):
return True return True
if newBase64Image == "-1": # and oldImageId != "-1"
if newBase64Image == "-1": # and str(oldImageId) != str(None)
return False return False
if oldImageId == "-1": # and newBase64Image != "-1"
if str(oldImageId) == str(None): # and newBase64Image != "-1"
return False return False
sql = "select * from market_images where image_id = %(imageId)s" sql = "select * from market_images where image_id = %(imageId)s"
@ -476,7 +476,7 @@ def is_same_image(newBase64Image, oldImageId):
recset = cur.fetchall() recset = cur.fetchall()
decImage = decode_decrypt_image_in_base64(base64Image)
decImage = decode_decrypt_image_in_base64(newBase64Image)
hash1 = generate_image_hash(decImage) hash1 = generate_image_hash(decImage)
hash2 = recset[0]['hash_image'] hash2 = recset[0]['hash_image']
@ -484,19 +484,21 @@ def is_same_image(newBase64Image, oldImageId):
if hash1 == hash2: if hash1 == hash2:
return True return True
features1 = json.loads(extract_hidden_layer_output(dec_string))
features1 = json.loads(extract_hidden_layer_output(decImage))
features2 = json.loads(recset[0]['resnet50_image']) features2 = json.loads(recset[0]['resnet50_image'])
similarity = 1 - distance.cosine(features1, features2) similarity = 1 - distance.cosine(features1, features2)
return similarity < 0.8
return similarity >= 0.8
def create_image(cur, base64Image, date): def create_image(cur, base64Image, date):
if base64Image == "-1": if base64Image == "-1":
return None return None
imageId = verifyImage(base64Image)
decImage = decode_decrypt_image_in_base64(base64Image)
imageId = verifyImage(cur, decImage)
if not imageId: if not imageId:
imageId = int(getLastImage(cur) + 1) imageId = int(getLastImage(cur) + 1)
@ -505,15 +507,13 @@ def create_image(cur, base64Image, date):
newImage = False newImage = False
if newImage: if newImage:
decImage = decode_decrypt_image_in_base64(base64Image)
hashImage = generate_image_hash(decImage) hashImage = generate_image_hash(decImage)
hloFeatures = extract_hidden_layer_output(decImage) hloFeatures = extract_hidden_layer_output(decImage)
keypoints, descriptors = extract_keypoints(decImage) keypoints, descriptors = extract_keypoints(decImage)
sql = "Insert into market_images(image_id, hash_image, base64_image, " \ sql = "Insert into market_images(image_id, hash_image, base64_image, " \
"resnet50_image, siftkey_image, siftdesc_image, dateinserted_image) " \
"resnet50_image, siftkey_image, siftdesc_image, dateinserted_image) " \
"Values (%s, %s, %s, %s, %s, %s, %s)" "Values (%s, %s, %s, %s, %s, %s, %s)"
recset = [imageId, recset = [imageId,
@ -556,11 +556,11 @@ def create_database(cur, con):
sql = "create table market_images(image_id integer not null, hash_image character varying(64) not null, base64_image character varying(10000000) not null, " \ sql = "create table market_images(image_id integer not null, hash_image character varying(64) not null, base64_image character varying(10000000) not null, " \
"resnet50_image character varying(1000000) null, siftkey_image character varying(1000000) null, siftdesc_image character varying(1000000) null, " \ "resnet50_image character varying(1000000) null, siftkey_image character varying(1000000) null, siftdesc_image character varying(1000000) null, " \
"dateinserted_item timestamp(6) with time zone not null, " \
"constraint items_pk primary key (image_id))"
"dateinserted_image timestamp(6) with time zone not null, " \
"constraint market_images_pk primary key (image_id))"
cur.execute(sql) cur.execute(sql)
sql = "create unique index unique_image ON market_images USING btree (hash_image ASC NULLS LAST)"
sql = "create unique index unique_market_image ON market_images USING btree (hash_image ASC NULLS LAST)"
cur.execute(sql) cur.execute(sql)
sql = "create table marketplaces(market_id integer not null, name_market character varying(255) not null, " \ sql = "create table marketplaces(market_id integer not null, name_market character varying(255) not null, " \


+ 3
- 0
MarketPlaces/Utilities/utilities.py View File

@ -89,6 +89,9 @@ def extract_keypoints(image_string):
keypoints, descriptors = sift.detectAndCompute(img, None) keypoints, descriptors = sift.detectAndCompute(img, None)
if len(keypoints) == 0:
return None, None
return json.dumps(wrap_keypoints(keypoints)), json.dumps(descriptors.tolist()) return json.dumps(wrap_keypoints(keypoints)), json.dumps(descriptors.tolist())


Loading…
Cancel
Save