Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

transforms.py 1.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2021. Jeffrey Nirschl. All rights reserved.
  3. #
  4. # Licensed under the MIT license. See the LICENSE file in the project
  5. # root directory for license information.
  6. #
  7. # Time-stamp: <>
  8. # ======================================================================
  9. import cv2
  10. import numpy as np
  11. import pandas as pd
  12. # Specify opencv optimization
  13. cv2.setUseOptimized(True)
  14. def mean_image(img_array, img_shape=(28, 28, 1)):
  15. """Accept images as numpy array with images separated by rows
  16. and columns indicating pixel values"""
  17. assert (type(img_array) is type(pd.DataFrame())), TypeError
  18. # pre-allocate mean_img
  19. mean_img = np.zeros(img_shape, dtype=np.float32)
  20. # process files
  21. print(f"Computing mean image...")
  22. for file_count, idx in enumerate(range(img_array.shape[0])):
  23. temp_img = np.reshape(img_array.iloc[idx].to_numpy(), img_shape)
  24. mean_img = cv2.accumulate(temp_img.astype(dtype=np.float32), mean_img)
  25. if file_count % 10000 == 0:
  26. print(f"\tProcessed {file_count:0d} images.")
  27. # divide by n_images
  28. mean_img = np.divide(mean_img, file_count + 1)
  29. return mean_img
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...