I'm trying to create a new column with a calculated value, this value is caclulated using methods inside a class. The class used to calculate is the following:
import functions_image_recognition as fir
class CompareImages():
def __init__(self, url_1, url_2):
self.img_url_1 = url_1
self.img_url_2 = url_2
def load_images(self, img_url, flag_color=False):
req = urllib.request.urlopen(img_url)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
image = cv2.imdecode(arr, -1) # Load image as it is
if not flag_color:
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Change color to greyscale
else:
return image # Original image
def main_process_ssmi(self):
imageA = self.load_images(self.img_url_1)
imageB = self.load_images(self.img_url_2)
(H, W) = imageA.shape
imageB = cv2.resize(imageB, (W, H))
(score, diff) = structural_similarity(imageA, imageB, full=True)
result = float("{}".format(score))
return result
The names of the columns in the dataframe are: seller_url,supplier_url
I want to create a new column in the dataframe with the result of applying the functions in the classes, and append the results to the original dataframe.
match_image = CompareImages(seller_url,supplier_url)
result = match_image.main_process_ssmi()
Using Apply you can use your existing class as follows.
Code
df['new_column'] = df.apply(lambda row: CompareImages(row['seller_url'], row['supplier_url']).main_process_ssmi(), axis = 1)
Explanation
We construct and object for each row and access the method main_process_ssmi.
In using:
row['seller_url'] and row['supplier_url']
The apply function passes row values, i.e. values for seller_url and supplier_url for each row.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments