kandi background
Explore Kits

vision | Datasets , Transforms and Models specific to Computer Vision | Computer Vision library

 by   pytorch Python Version: v0.12.0 License: BSD-3-Clause

 by   pytorch Python Version: v0.12.0 License: BSD-3-Clause

Download this library from

kandi X-RAY | vision Summary

vision is a Python library typically used in Artificial Intelligence, Computer Vision, Deep Learning, Pytorch, Tensorflow applications. vision has no bugs, it has no vulnerabilities, it has build file available, it has a Permissive License and it has medium support. You can download it from GitHub, Maven.
Datasets, Transforms and Models specific to Computer Vision
Support
Support
Quality
Quality
Security
Security
License
License
Reuse
Reuse

kandi-support Support

  • vision has a medium active ecosystem.
  • It has 11461 star(s) with 5875 fork(s). There are 365 watchers for this library.
  • There were 5 major release(s) in the last 12 months.
  • There are 545 open issues and 1729 have been closed. On average issues are closed in 50 days. There are 154 open pull requests and 0 closed requests.
  • It has a neutral sentiment in the developer community.
  • The latest version of vision is v0.12.0
vision Support
Best in #Computer Vision
Average in #Computer Vision
vision Support
Best in #Computer Vision
Average in #Computer Vision

quality kandi Quality

  • vision has 0 bugs and 0 code smells.
vision Quality
Best in #Computer Vision
Average in #Computer Vision
vision Quality
Best in #Computer Vision
Average in #Computer Vision

securitySecurity

  • vision has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
  • vision code analysis shows 0 unresolved vulnerabilities.
  • There are 0 security hotspots that need review.
vision Security
Best in #Computer Vision
Average in #Computer Vision
vision Security
Best in #Computer Vision
Average in #Computer Vision

license License

  • vision is licensed under the BSD-3-Clause License. This license is Permissive.
  • Permissive licenses have the least restrictions, and you can use them in most projects.
vision License
Best in #Computer Vision
Average in #Computer Vision
vision License
Best in #Computer Vision
Average in #Computer Vision

buildReuse

  • vision releases are available to install and integrate.
  • Deployable package is available in Maven.
  • Build file is available. You can build the component from source.
  • vision saves you 11390 person hours of effort in developing the same functionality from scratch.
  • It has 31456 lines of code, 2354 functions and 200 files.
  • It has medium code complexity. Code complexity directly impacts maintainability of the code.
vision Reuse
Best in #Computer Vision
Average in #Computer Vision
vision Reuse
Best in #Computer Vision
Average in #Computer Vision
Top functions reviewed by kandi - BETA

kandi has reviewed vision and discovered the below as its top functions. This is intended to give you an instant insight into vision implemented functionality, and help decide if they suit your requirements.

  • Return a list of all supported extensions
  • Create a feature extractor .
  • Draws affine transformation .
  • Uses SSDL Lite320 .
  • Wrapper for FasterRCNN .
  • Generate a grid of tensors .
  • Read video from memory .
  • Construct an SSD 2000 model .
  • Construct a KeypointRCNN .
  • Apply a transformation to an image .

vision Key Features

Datasets, Transforms and Models specific to Computer Vision

Create new boolean fields based on specific bigrams appearing in a tokenized pandas dataframe

copy iconCopydownload iconDownload
regex = '|'.join('(%s)' % b.replace(' ', r'\s+') for b in bigrams)
matches = (df['job_description'].apply(' '.join)
           .str.extractall(regex).droplevel(1).notna()
           .groupby(level=0).max()
           )
matches.columns = bigrams

out = df.join(matches).fillna(False)
                         job_description  data science  computer science  bachelors degree
0        [data, science, degree, expert]          True             False             False
1   [computer, science, degree, masters]         False              True             False
2  [bachelors, degree, computer, vision]         False             False              True
3            [data, processing, science]         False             False             False
'(data\\s+science)|(computer\\s+science)|(bachelors\\s+degree)'
-----------------------
regex = '|'.join('(%s)' % b.replace(' ', r'\s+') for b in bigrams)
matches = (df['job_description'].apply(' '.join)
           .str.extractall(regex).droplevel(1).notna()
           .groupby(level=0).max()
           )
matches.columns = bigrams

out = df.join(matches).fillna(False)
                         job_description  data science  computer science  bachelors degree
0        [data, science, degree, expert]          True             False             False
1   [computer, science, degree, masters]         False              True             False
2  [bachelors, degree, computer, vision]         False             False              True
3            [data, processing, science]         False             False             False
'(data\\s+science)|(computer\\s+science)|(bachelors\\s+degree)'
-----------------------
regex = '|'.join('(%s)' % b.replace(' ', r'\s+') for b in bigrams)
matches = (df['job_description'].apply(' '.join)
           .str.extractall(regex).droplevel(1).notna()
           .groupby(level=0).max()
           )
matches.columns = bigrams

out = df.join(matches).fillna(False)
                         job_description  data science  computer science  bachelors degree
0        [data, science, degree, expert]          True             False             False
1   [computer, science, degree, masters]         False              True             False
2  [bachelors, degree, computer, vision]         False             False              True
3            [data, processing, science]         False             False             False
'(data\\s+science)|(computer\\s+science)|(bachelors\\s+degree)'
-----------------------
import pandas as pd
import numpy as np
import nltk

bigrams = ['data science', 'computer science', 'bachelors degree']
df = pd.DataFrame(data={'job_description': [['data', 'science', 'degree', 'expert'],
                                            ['computer', 'science', 'degree', 'masters'],
                                            ['bachelors', 'degree', 'computer', 'vision'],
                                            ['data', 'processing', 'science']]})

def find_bigrams(data):
  output = np.zeros((data.shape[0], len(bigrams)), dtype=bool)
  for i, d in enumerate(data):
    possible_bigrams = [' '.join(x) for x in list(nltk.bigrams(d)) + list(nltk.bigrams(d[::-1]))]
    indices = np.where(np.isin(bigrams, list(set(bigrams).intersection(set(possible_bigrams)))))
    output[i, indices] = True
  return list(output.T)

output = find_bigrams(df['job_description'].to_numpy())
df = df.assign(**dict(zip(bigrams, output)))
|    | job_description                               | data science   | computer science   | bachelors degree   |
|---:|:----------------------------------------------|:---------------|:-------------------|:-------------------|
|  0 | ['data', 'science', 'degree', 'expert']       | True           | False              | False              |
|  1 | ['computer', 'science', 'degree', 'masters']  | False          | True               | False              |
|  2 | ['bachelors', 'degree', 'computer', 'vision'] | False          | False              | True               |
|  3 | ['data', 'processing', 'science']             | False          | False              | False              |
-----------------------
import pandas as pd
import numpy as np
import nltk

bigrams = ['data science', 'computer science', 'bachelors degree']
df = pd.DataFrame(data={'job_description': [['data', 'science', 'degree', 'expert'],
                                            ['computer', 'science', 'degree', 'masters'],
                                            ['bachelors', 'degree', 'computer', 'vision'],
                                            ['data', 'processing', 'science']]})

def find_bigrams(data):
  output = np.zeros((data.shape[0], len(bigrams)), dtype=bool)
  for i, d in enumerate(data):
    possible_bigrams = [' '.join(x) for x in list(nltk.bigrams(d)) + list(nltk.bigrams(d[::-1]))]
    indices = np.where(np.isin(bigrams, list(set(bigrams).intersection(set(possible_bigrams)))))
    output[i, indices] = True
  return list(output.T)

output = find_bigrams(df['job_description'].to_numpy())
df = df.assign(**dict(zip(bigrams, output)))
|    | job_description                               | data science   | computer science   | bachelors degree   |
|---:|:----------------------------------------------|:---------------|:-------------------|:-------------------|
|  0 | ['data', 'science', 'degree', 'expert']       | True           | False              | False              |
|  1 | ['computer', 'science', 'degree', 'masters']  | False          | True               | False              |
|  2 | ['bachelors', 'degree', 'computer', 'vision'] | False          | False              | True               |
|  3 | ['data', 'processing', 'science']             | False          | False              | False              |

How do I remove background from an image like this?

copy iconCopydownload iconDownload
import cv2
import numpy as np

def process(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_blur = cv2.GaussianBlur(img_gray, (3, 3), 2)
    img_canny = cv2.Canny(img_blur, 50, 9)
    img_dilate = cv2.dilate(img_canny, np.ones((4, 2)), iterations=11)
    img_erode = cv2.erode(img_dilate, np.ones((13, 7)), iterations=4)
    return cv2.bitwise_not(img_erode)

def get_contours(img):
    contours, _ = cv2.findContours(process(img), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    cnt = max(contours, key=cv2.contourArea)
    cv2.drawContours(img, [cv2.convexHull(cnt)], -1, (0, 0, 255), 2)

img = cv2.imread("image2.png")
get_contours(img)
cv2.imshow("result", img)

cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np

def process(img, b_k, b_s, c_t1, c_t2, k1, k2, k3, k4, iter1, iter2):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    b_k = b_k // 2 * 2 + 1
    img_blur = cv2.GaussianBlur(img_gray, (b_k, b_k), b_s)
    img_canny = cv2.Canny(img_blur, c_t1, c_t2)
    img_dilate = cv2.dilate(img_canny, np.ones((k1, k2)), iterations=iter1)
    img_erode = cv2.erode(img_dilate, np.ones((k3, k4)), iterations=iter2)
    return cv2.bitwise_not(img_erode)

d = {"Blur Kernel": (3, 50),
     "Blur Sigma": (2, 30),
     "Canny Threshold 1": (50, 500),
     "Canny Threshold 2": (9, 500),
     "Dilate Kernel1": (4, 50),
     "Dilate Kernel2": (2, 50),
     "Erode Kernel1": (13, 50),
     "Erode Kernel2": (7, 50),
     "Dilate Iterations": (11, 40),
     "Erode Iterations": (4, 40)}

cv2.namedWindow("Track Bars")
for i in d:
    cv2.createTrackbar(i, "Track Bars", *d[i], id)

img = cv2.imread("image1.png")

while True:
    img_copy = img.copy()
    processed = process(img, *(cv2.getTrackbarPos(i, "Track Bars") for i in d))
    contours, _ = cv2.findContours(processed, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    if contours:
        cnt = max(contours, key=cv2.contourArea)
        cv2.drawContours(img_copy, [cv2.convexHull(cnt)], -1, (0, 0, 255), 2)
    cv2.imshow("result", img_copy)
    
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

cv2.waitKey(0)
cv2.destroyAllWindows()
-----------------------
import cv2
import numpy as np

def process(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_blur = cv2.GaussianBlur(img_gray, (3, 3), 2)
    img_canny = cv2.Canny(img_blur, 50, 9)
    img_dilate = cv2.dilate(img_canny, np.ones((4, 2)), iterations=11)
    img_erode = cv2.erode(img_dilate, np.ones((13, 7)), iterations=4)
    return cv2.bitwise_not(img_erode)

def get_contours(img):
    contours, _ = cv2.findContours(process(img), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    cnt = max(contours, key=cv2.contourArea)
    cv2.drawContours(img, [cv2.convexHull(cnt)], -1, (0, 0, 255), 2)

img = cv2.imread("image2.png")
get_contours(img)
cv2.imshow("result", img)

cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np

def process(img, b_k, b_s, c_t1, c_t2, k1, k2, k3, k4, iter1, iter2):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    b_k = b_k // 2 * 2 + 1
    img_blur = cv2.GaussianBlur(img_gray, (b_k, b_k), b_s)
    img_canny = cv2.Canny(img_blur, c_t1, c_t2)
    img_dilate = cv2.dilate(img_canny, np.ones((k1, k2)), iterations=iter1)
    img_erode = cv2.erode(img_dilate, np.ones((k3, k4)), iterations=iter2)
    return cv2.bitwise_not(img_erode)

d = {"Blur Kernel": (3, 50),
     "Blur Sigma": (2, 30),
     "Canny Threshold 1": (50, 500),
     "Canny Threshold 2": (9, 500),
     "Dilate Kernel1": (4, 50),
     "Dilate Kernel2": (2, 50),
     "Erode Kernel1": (13, 50),
     "Erode Kernel2": (7, 50),
     "Dilate Iterations": (11, 40),
     "Erode Iterations": (4, 40)}

cv2.namedWindow("Track Bars")
for i in d:
    cv2.createTrackbar(i, "Track Bars", *d[i], id)

img = cv2.imread("image1.png")

while True:
    img_copy = img.copy()
    processed = process(img, *(cv2.getTrackbarPos(i, "Track Bars") for i in d))
    contours, _ = cv2.findContours(processed, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    if contours:
        cnt = max(contours, key=cv2.contourArea)
        cv2.drawContours(img_copy, [cv2.convexHull(cnt)], -1, (0, 0, 255), 2)
    cv2.imshow("result", img_copy)
    
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

cv2.waitKey(0)
cv2.destroyAllWindows()
-----------------------
import cv2 
import numpy as np
from google.colab.patches import cv2_imshow

img = cv2.imread("reference_1.png", cv2.IMREAD_GRAYSCALE) 
frame = cv2.imread("top.png", cv2.IMREAD_GRAYSCALE)

# if SIFT_create() gives problems, try downgrading opencv with
# pip uninstall opencv-python
# pip install opencv-contrib-python==3.4.2.17
sift = cv2.xfeatures2d.SIFT_create() 
kp_image, desc_image = sift.detectAndCompute(img, None) 
kp_frame, desc_frame = sift.detectAndCompute(frame, None) 

index_params = dict(algorithm=0, trees=5) 
search_params = dict() 
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(desc_image, desc_frame, k=2)

# clean the matches
good_points=[] 
for m, n in matches: 
    if(m.distance < 0.6 * n.distance): 
        good_points.append(m)

query_pts = np.float32([kp_image[m.queryIdx].pt for m in good_points]).reshape(-1, 1, 2) 
train_pts = np.float32([kp_frame[m.trainIdx].pt for m in good_points]).reshape(-1, 1, 2)

# find homography to find mask
matrix, mask = cv2.findHomography(query_pts, train_pts, cv2.RANSAC, 5.0) 
matches_mask = mask.ravel().tolist()
h,w = img.shape
pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
dst = cv2.perspectiveTransform(pts, matrix)
homography = cv2.polylines(frame, [np.int32(dst)], True, (255, 0, 0), 3) 

cv2_imshow(homography) 

After updating Gradle to 7.0.2, Element type “manifest” must be followed by either attribute specifications, “&gt;” or “/&gt;” error

copy iconCopydownload iconDownload
implementation 'com.huawei.agconnect:agconnect-core:1.4.0.300'
-----------------------
implementation 'com.huawei.agconnect:agcp:1.5.2.300'
-----------------------
implementation 'com.huawei.agconnect:agconnect-core:1.5.2.307'
implementation 'com.huawei.agconnect:agconnect-core:1.5.2.300'
-----------------------
implementation 'com.huawei.agconnect:agconnect-core:1.5.2.307'
implementation 'com.huawei.agconnect:agconnect-core:1.5.2.300'

How to get post categories from an object in array

copy iconCopydownload iconDownload
const arr = [
    {
      "category": 1,
      "content": "For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.",
      "content_preview": null,
      "coverImageSubtitle": null,
      "coverImageTitle": null,
      "created_at": "2021-05-13T18:34:17.260Z",
      "custom_link": null,
      "gallery": [],
      "id": 1,
      "isDeleted": null,
      "locale": "sq",
      "localizations": [],
      "pages": [
        {
          "commitments": null,
          "content_sub_menu": null,
          "created_at": "2021-05-19T21:19:15.101Z",
          "id": 18,
          "isActive": true,
          "locale": "sq",
          "name": "Member Listing",
          "showOnFooterMenu": null,
          "showOnMainMenu": null,
          "slug": "member-listing",
          "template": "member-listing",
          "updated_at": "2021-09-28T09:29:56.383Z",
        },
        {
          "commitments": null,
          "content_sub_menu": null,
          "created_at": "2021-05-19T21:33:28.500Z",
          "id": 22,
          "isActive": true,
          "locale": "sq",
          "name": "Evaluation Process / AL",
          "showOnFooterMenu": null,
          "showOnMainMenu": null,
          "slug": "evaluation-process",
          "template": "join-now",
          "updated_at": "2021-05-19T21:33:29.331Z",
        },
      ],
      "post_categories": [
        {
          "created_at": "2021-05-20T15:54:36.949Z",
          "id": 1,
          "locale": "sq",
          "name": "Lajmet",
          "published_at": "2021-05-20T15:54:45.222Z",
          "updated_at": "2021-05-20T15:54:46.179Z",
        },
        {
          "created_at": "2021-05-20T15:54:36.949Z",
          "id": 2,
          "locale": "sq",
          "name": "Lajmet",
          "published_at": "2021-05-20T15:54:45.222Z",
          "updated_at": "2021-05-20T15:54:46.179Z",
        },
      ],
      "published_at": "2021-05-13T18:34:17.260Z",
      "slider": [
        {
          "fixed": null,
          "id": 6,
          "image": [
            {
              "alternativeText": "",
              "caption": "",
              "created_at": "2021-05-13T18:34:10.259Z",
              "ext": ".svg",
              "formats": null,
              "hash": "feature_image_297dbc6cd8",
              "height": 450,
              "id": 11,
              "mime": "image/svg+xml",
              "name": "feature-image.svg",
              "previewUrl": null,
              "provider": "local",
              "provider_metadata": null,
              "size": 54.94,
              "updated_at": "2021-05-13T18:34:10.522Z",
              "url": "/uploads/feature_image_297dbc6cd8.svg",
              "width": 600,
            },
          ],
          "parallax": null,
        },
      ],
      "sort": null,
      "subTitle": null,
      "thumbnail": null,
      "title": "Our Vision, Mission and Guiding Principles",
      "updated_at": "2021-06-22T09:27:39.155Z",
    }]

  console.log(arr.filter(el => el.post_categories.length > 0 && el.id === el.post_categories[0]?.id))
  // console.log(arr.map(el => ({...el, post_categories: el.post_categories.filter(post => post.id === el.category)})))
  

How do you create a new AVAsset video that consists of only frames from given `CMTimeRange`s of another video?

copy iconCopydownload iconDownload
import UIKit
import AVFoundation
import Vision

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
    let session = AVCaptureSession()
    
    var assetWriter: AVAssetWriter!
    var assetWriterInput: AVAssetWriterInput!
    var assetWriterStartTime: CMTime = .zero
    var assetWriterStarted = false

    var referenceFileURL: URL!
    var timeRangesOfInterest: [Double : CMTimeRange] = [:]

    func startWritingFile(outputURL: URL, initialSampleBuffer: CMSampleBuffer) {
        try? FileManager.default.removeItem(at: outputURL)
        assetWriter = try! AVAssetWriter(outputURL: outputURL, fileType: .mov)

        let dimensions = initialSampleBuffer.formatDescription!.dimensions
        assetWriterInput = AVAssetWriterInput(mediaType: .video, outputSettings: [AVVideoCodecKey: AVVideoCodecType.h264, AVVideoWidthKey: dimensions.width, AVVideoHeightKey: dimensions.height])
        
        assetWriter.add(assetWriterInput)

        assetWriter.startWriting()
        
        self.assetWriterStartTime = CMSampleBufferGetPresentationTimeStamp(initialSampleBuffer)
        assetWriter.startSession(atSourceTime: self.assetWriterStartTime)
    }
    
    func stopWritingFile(completion: @escaping (() -> Void)) {
        let assetWriterToFinish = self.assetWriter!
        self.assetWriterInput = nil
        self.assetWriter = nil
                
        assetWriterToFinish.finishWriting {
            print("finished writing: \(assetWriterToFinish.status.rawValue)")
            completion()
        }
    }
    
    func exportVideoTimeRanges(inputFileURL: URL, outputFileURL: URL, timeRanges: [CMTimeRange]) {
        let inputAsset = AVURLAsset(url: inputFileURL)
        let inputVideoTrack = inputAsset.tracks(withMediaType: .video).first!
        
        let composition = AVMutableComposition()
        
        let compositionTrack = composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)!
        
        var insertionPoint: CMTime = .zero
        for timeRange in timeRanges {
            try! compositionTrack.insertTimeRange(timeRange, of: inputVideoTrack, at: insertionPoint)
            insertionPoint = insertionPoint + timeRange.duration
        }
        
        let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)!
        try? FileManager.default.removeItem(at: outputFileURL)
        exportSession.outputURL = outputFileURL
        exportSession.outputFileType = .mov
        exportSession.exportAsynchronously {
            print("export finished: \(exportSession.status.rawValue) - \(exportSession.error)")
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
                
        let inputDevice = AVCaptureDevice.default(for: .video)!
        let input = try! AVCaptureDeviceInput(device: inputDevice)
        let output = AVCaptureVideoDataOutput()
        
        output.setSampleBufferDelegate(self, queue: DispatchQueue.main)
        
        session.addInput(input)
        session.addOutput(output)
        
        session.startRunning()
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 20) {
            self.stopWritingFile {
                print("finished writing")
                
                let trajectoriesFileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] .appendingPathComponent("trajectories.mov")

                self.exportVideoTimeRanges(inputFileURL: self.referenceFileURL, outputFileURL: trajectoriesFileURL, timeRanges: self.timeRangesOfInterest.map { $0.1 })
            }
        }
    }
    
    // Lazily create a single instance of VNDetectTrajectoriesRequest.
    private lazy var request: VNDetectTrajectoriesRequest = {
        return VNDetectTrajectoriesRequest(frameAnalysisSpacing: .zero,
                                           trajectoryLength: 10,
                                           completionHandler: completionHandler)
    }()
    
    // AVCaptureVideoDataOutputSampleBufferDelegate callback.
    func captureOutput(_ output: AVCaptureOutput,
                       didOutput sampleBuffer: CMSampleBuffer,
                       from connection: AVCaptureConnection) {
        if !assetWriterStarted {
            self.referenceFileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] .appendingPathComponent("reference.mov")

            startWritingFile(outputURL: self.referenceFileURL, initialSampleBuffer: sampleBuffer)
            assetWriterStarted = true
        }
        
        if assetWriterInput != nil && assetWriterInput.isReadyForMoreMediaData {
            assetWriterInput.append(sampleBuffer)
        }
        
        do {
            let requestHandler = VNImageRequestHandler(cmSampleBuffer: sampleBuffer)
            try requestHandler.perform([request])
        } catch {
            // Handle the error.
        }
    }
    
    func completionHandler(request: VNRequest, error: Error?) {
        guard let request = request as? VNDetectTrajectoriesRequest else { return }

        if let results = request.results,
           results.count > 0 {
            NSLog("\(results)")
            for result in results {
                var fileRelativeTimeRange = result.timeRange
                fileRelativeTimeRange.start = fileRelativeTimeRange.start - self.assetWriterStartTime
                self.timeRangesOfInterest[fileRelativeTimeRange.start.seconds] = fileRelativeTimeRange
            }
        }
    }
}

How do you implement React-native-gesture-handler with React Navigation 6.x Native Stack Navigator (RN&gt;0.6)?

copy iconCopydownload iconDownload
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Home" component={gestureHandlerRootHOC(HomeScreen)} />
-----------------------
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Home" component={gestureHandlerRootHOC(HomeScreen)} />

Is there any reason for changing the channels order of an image from RGB to BGR?

copy iconCopydownload iconDownload
def load_video(path, max_frames=0):
    cap = cv2.VideoCapture(path)
    frames = []
    try:
        while True:
            ret, frame = cap.read()
            if not ret:
                break
            frame = crop_center(frame)
            frame = frame[:, :, [2, 1, 0]]
            frames.append(frame)

            if len(frames) == max_frames:
                break
    finally:
        cap.release()
    return np.array(frames)

InvalidArgumentError: Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [4], [batch]: [5] [Op:IteratorGetNext]

copy iconCopydownload iconDownload
!pip install unrar
!unrar x /content/captcha_caps.rar

import os
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path
from collections import Counter
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

data_dir = Path("/content/captcha_caps/")

images = sorted(list(map(str, list(data_dir.glob("*.PNG")))))
labels = [img.split(os.path.sep)[-1].split(".PNG")[0] for img in images]
characters = set(char for label in labels for char in label)

print("Number of images found: ", len(images))
print("Number of labels found: ", len(labels))
print("Number of unique characters: ", len(characters))
print("Characters present: ", characters)

batch_size = 16

img_width = 200
img_height = 50

downsample_factor = 4

max_length = max([len(label) for label in labels])
char_to_num = layers.StringLookup(
    vocabulary=list(characters), mask_token=None
)

num_to_char = layers.StringLookup(
    vocabulary=char_to_num.get_vocabulary(), mask_token=None, invert=True
)

def split_data(images, labels, train_size=0.9, shuffle=True):
    size = len(images)
    indices = np.arange(size)
    if shuffle:
        np.random.shuffle(indices)
    train_samples = int(size * train_size)
    x_train, y_train = images[indices[:train_samples]], labels[indices[:train_samples]]
    x_valid, y_valid = images[indices[train_samples:]], labels[indices[train_samples:]]
    return x_train, x_valid, y_train, y_valid

x_train, x_valid, y_train, y_valid = split_data(np.array(images), np.array(labels))


def encode_single_sample(img_path, label):
    img = tf.io.read_file(img_path)
    img = tf.io.decode_png(img, channels=1)
    img = tf.image.convert_image_dtype(img, tf.float32)
    img = tf.image.resize(img, [img_height, img_width])
    img = tf.transpose(img, perm=[1, 0, 2])
    label = char_to_num(tf.strings.unicode_split(label, input_encoding="UTF-8"))
    return {"image": img, "label": label}

train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_dataset = (
    train_dataset.map(
        encode_single_sample, num_parallel_calls=tf.data.AUTOTUNE
    )
    .batch(batch_size)
    .prefetch(buffer_size=tf.data.AUTOTUNE).repeat(10)
)

validation_dataset = tf.data.Dataset.from_tensor_slices((x_valid, y_valid))
validation_dataset = (
    validation_dataset.map(
        encode_single_sample, num_parallel_calls=tf.data.AUTOTUNE
    )
    .batch(batch_size)
    .prefetch(buffer_size=tf.data.AUTOTUNE)
)

_, ax = plt.subplots(4, 4, figsize=(10, 5))
for batch in validation_dataset.take(1):
    images = batch["image"]
    labels = batch["label"]
    for i in range(16):
        img = (images[i] * 255).numpy().astype("uint8")
        label = tf.strings.reduce_join(num_to_char(labels[i])).numpy().decode("utf-8")
        ax[i // 4, i % 4].imshow(img[:, :, 0].T, cmap="gray")
        ax[i // 4, i % 4].set_title(label)
        ax[i // 4, i % 4].axis("off")
plt.show()
Requirement already satisfied: unrar in /usr/local/lib/python3.7/dist-packages (0.4)

UNRAR 5.50 freeware      Copyright (c) 1993-2017 Alexander Roshal


Extracting from /content/captcha_caps.rar

Creating    captcha caps                                              OK
Extracting  captcha caps/24VCZ.PNG                                    OK 
Extracting  captcha caps/26SGX.PNG                                    OK 
Extracting  captcha caps/2HC5E.PNG                                    OK 
Extracting  captcha caps/2NDXL.PNG                                    OK 
Extracting  captcha caps/2NUEH.PNG                                    OK 
Extracting  captcha caps/2QX4B.PNG                                    OK 
Extracting  captcha caps/2V78Y.PNG                                    OK 
Extracting  captcha caps/2Z45Y.PNG                                    OK 
Extracting  captcha caps/2Z9R2.PNG                                    OK 
Extracting  captcha caps/32HZA.PNG                                    OK 
Extracting  captcha caps/38JKT.PNG                                    OK 
Extracting  captcha caps/39EZ4.PNG                                    OK 
Extracting  captcha caps/3GJ85.PNG                                    OK 
Extracting  captcha caps/3R2JE.PNG                                    OK 
Extracting  captcha caps/3RU4C.PNG                                    OK 
Extracting  captcha caps/3TPFA.PNG                                    OK 
Extracting  captcha caps/3TVAC.PNG                                    OK 
Extracting  captcha caps/44U8C.PNG                                    OK 
Extracting  captcha caps/452LV.PNG                                    OK 
Extracting  captcha caps/4E4P8.PNG                                    OK 
Extracting  captcha caps/4E5HX.PNG                                    OK 
Extracting  captcha caps/4FVS7.PNG                                    OK 
Extracting  captcha caps/4GJCC.PNG                                    OK 
Extracting  captcha caps/4QQJD.PNG                                    OK 
Extracting  captcha caps/4TH2K.PNG                                    OK 
Extracting  captcha caps/4TN2L.PNG                                    OK 
Extracting  captcha caps/4YBT5.PNG                                    OK 
Extracting  captcha caps/4ZLHE.PNG                                    OK 
Extracting  captcha caps/556F5.PNG                                    OK 
Extracting  captcha caps/55DT5.PNG                                    OK 
Extracting  captcha caps/5CEZD.PNG                                    OK 
Extracting  captcha caps/5CQ39.PNG                                    OK 
Extracting  captcha caps/5FZUR.PNG                                    OK 
Extracting  captcha caps/5H7F4.PNG                                    OK 
Extracting  captcha caps/5K4TY.PNG                                    OK 
Extracting  captcha caps/5N2KC.PNG                                    OK 
Extracting  captcha caps/5P6B4.PNG                                    OK 
Extracting  captcha caps/5R728.PNG                                    OK 
Extracting  captcha caps/5S9E7.PNG                                    OK 
Extracting  captcha caps/5VRRV.PNG                                    OK 
Extracting  captcha caps/5VZHL.PNG                                    OK 
Extracting  captcha caps/5YVYG.PNG                                    OK 
Extracting  captcha caps/63P4N.PNG                                    OK 
Extracting  captcha caps/65DQ7.PNG                                    OK 
Extracting  captcha caps/66JUU.PNG                                    OK 
Extracting  captcha caps/69ZQ3.PNG                                    OK 
Extracting  captcha caps/6B655.PNG                                    OK 
Extracting  captcha caps/6GBFG.PNG                                    OK 
Extracting  captcha caps/6K27H.PNG                                    OK 
Extracting  captcha caps/6R7G5.PNG                                    OK 
Extracting  captcha caps/6VFYG.PNG                                    OK 
Extracting  captcha caps/6X8AJ.PNG                                    OK 
Extracting  captcha caps/6ZNJP.PNG                                    OK 
Extracting  captcha caps/73ZK2.PNG                                    OK 
Extracting  captcha caps/74FPR.PNG                                    OK 
Extracting  captcha caps/7C46N.PNG                                    OK 
Extracting  captcha caps/7C48B.PNG                                    OK 
Extracting  captcha caps/7JVBT.PNG                                    OK 
Extracting  captcha caps/7NVS8.PNG                                    OK 
Extracting  captcha caps/7REZP.PNG                                    OK 
Extracting  captcha caps/7RHSQ.PNG                                    OK 
Extracting  captcha caps/7RTT2.PNG                                    OK 
Extracting  captcha caps/7VV9J.PNG                                    OK 
Extracting  captcha caps/82JNK.PNG                                    OK 
Extracting  captcha caps/83JKQ.PNG                                    OK 
Extracting  captcha caps/89RGK.PNG                                    OK 
Extracting  captcha caps/8A2D7.PNG                                    OK 
Extracting  captcha caps/8ENGQ.PNG                                    OK 
Extracting  captcha caps/8K5KS.PNG                                    OK 
Extracting  captcha caps/95BDX.PNG                                    OK 
Extracting  captcha caps/963D9.PNG                                    OK 
Extracting  captcha caps/9878H.PNG                                    OK 
Extracting  captcha caps/99G9R.PNG                                    OK 
Extracting  captcha caps/99RJ8.PNG                                    OK 
Extracting  captcha caps/9CKGT.PNG                                    OK 
Extracting  captcha caps/9DK36.PNG                                    OK 
Extracting  captcha caps/9E3FU.PNG                                    OK 
Extracting  captcha caps/9EZCJ.PNG                                    OK 
Extracting  captcha caps/9HS3T.PNG                                    OK 
Extracting  captcha caps/9J59G.PNG                                    OK 
Extracting  captcha caps/9JXEJ.PNG                                    OK 
Extracting  captcha caps/9TBBF.PNG                                    OK 
Extracting  captcha caps/9TYDP.PNG                                    OK 
Extracting  captcha caps/9YEY2.PNG                                    OK 
Extracting  captcha caps/A6TC6.PNG                                    OK 
Extracting  captcha caps/ADB8Y.PNG                                    OK 
Extracting  captcha caps/AERBR.PNG                                    OK 
Extracting  captcha caps/AG43G.PNG                                    OK 
Extracting  captcha caps/ALX5Q.PNG                                    OK 
Extracting  captcha caps/AP6EJ.PNG                                    OK 
Extracting  captcha caps/AUFH4.PNG                                    OK 
Extracting  captcha caps/AVAYP.PNG                                    OK 
Extracting  captcha caps/AX2QR.PNG                                    OK 
Extracting  captcha caps/AZS3U.PNG                                    OK 
Extracting  captcha caps/B6ZYP.PNG                                    OK 
Extracting  captcha caps/B8YTF.PNG                                    OK 
Extracting  captcha caps/BEGC2.PNG                                    OK 
Extracting  captcha caps/BQFXZ.PNG                                    OK 
Extracting  captcha caps/BQSB2.PNG                                    OK 
Extracting  captcha caps/BT5CN.PNG                                    OK 
Extracting  captcha caps/BYJL9.PNG                                    OK 
Extracting  captcha caps/BZYB7.PNG                                    OK 
Extracting  captcha caps/C2EFS.PNG                                    OK 
Extracting  captcha caps/C3T9L.PNG                                    OK 
Extracting  captcha caps/C8C26.PNG                                    OK 
Extracting  captcha caps/CACQC.PNG                                    OK 
Extracting  captcha caps/CBXJY.PNG                                    OK 
Extracting  captcha caps/CE6S8.PNG                                    OK 
Extracting  captcha caps/CEFCR.PNG                                    OK 
Extracting  captcha caps/CEPQV.PNG                                    OK 
Extracting  captcha caps/CF3V8.PNG                                    OK 
Extracting  captcha caps/CFR3R.PNG                                    OK 
Extracting  captcha caps/CKEQK.PNG                                    OK 
Extracting  captcha caps/CUD8R.PNG                                    OK 
Extracting  captcha caps/D2ZSU.PNG                                    OK 
Extracting  captcha caps/D56EX.PNG                                    OK 
Extracting  captcha caps/DBAAX.PNG                                    OK 
Extracting  captcha caps/DC2AV.PNG                                    OK 
Extracting  captcha caps/DDZRZ.PNG                                    OK 
Extracting  captcha caps/DF266.PNG                                    OK 
Extracting  captcha caps/DGLYX.PNG                                    OK 
Extracting  captcha caps/DNQ8C.PNG                                    OK 
Extracting  captcha caps/DPQCC.PNG                                    OK 
Extracting  captcha caps/DUU3R.PNG                                    OK 
Extracting  captcha caps/DY935.PNG                                    OK 
Extracting  captcha caps/DYE9U.PNG                                    OK 
Extracting  captcha caps/E6RVE.PNG                                    OK 
Extracting  captcha caps/E7B47.PNG                                    OK 
Extracting  captcha caps/EB975.PNG                                    OK 
Extracting  captcha caps/EHQVT.PNG                                    OK 
Extracting  captcha caps/EJB7K.PNG                                    OK 
Extracting  captcha caps/EJEUJ.PNG                                    OK 
Extracting  captcha caps/EN3SG.PNG                                    OK 
Extracting  captcha caps/EQP2Q.PNG                                    OK 
Extracting  captcha caps/ESLUT.PNG                                    OK 
Extracting  captcha caps/ET497.PNG                                    OK 
Extracting  captcha caps/F2GTJ.PNG                                    OK 
Extracting  captcha caps/F32UK.PNG                                    OK 
Extracting  captcha caps/F8B56.PNG                                    OK 
Extracting  captcha caps/FEQRA.PNG                                    OK 
Extracting  captcha caps/FF5AZ.png                                    OK 
Extracting  captcha caps/FGBBV.PNG                                    OK 
Extracting  captcha caps/FN4XQ.PNG                                    OK 
Extracting  captcha caps/FUHZJ.PNG                                    OK 
Extracting  captcha caps/FZBZB.PNG                                    OK 
Extracting  captcha caps/G3C7R.PNG                                    OK 
Extracting  captcha caps/G3H2V.PNG                                    OK 
Extracting  captcha caps/G5A9V.PNG                                    OK 
Extracting  captcha caps/G9CLN.PNG                                    OK 
Extracting  captcha caps/GAGZG.PNG                                    OK 
Extracting  captcha caps/GCRFA.PNG                                    OK 
Extracting  captcha caps/GF59Q.PNG                                    OK 
Extracting  captcha caps/GGFJH.PNG                                    OK 
Extracting  captcha caps/GHNPE.PNG                                    OK 
Extracting  captcha caps/GKGQ5.PNG                                    OK 
Extracting  captcha caps/GLNPR.PNG                                    OK 
Extracting  captcha caps/GRKU3.PNG                                    OK 
Extracting  captcha caps/GXGEA.PNG                                    OK 
Extracting  captcha caps/H2JTQ.PNG                                    OK 
Extracting  captcha caps/HAF8J.PNG                                    OK 
Extracting  captcha caps/HDK86.PNG                                    OK 
Extracting  captcha caps/HLG6G.PNG                                    OK 
Extracting  captcha caps/HQEFG.PNG                                    OK 
Extracting  captcha caps/HUG3K.PNG                                    OK 
Extracting  captcha caps/HZBJ9.PNG                                    OK 
Extracting  captcha caps/J38T7.PNG                                    OK 
Extracting  captcha caps/J3DG2.PNG                                    OK 
Extracting  captcha caps/J5PE7.PNG                                    OK 
Extracting  captcha caps/J72PD.PNG                                    OK 
Extracting  captcha caps/J8E5Z.PNG                                    OK 
Extracting  captcha caps/JC5YF.PNG                                    OK 
Extracting  captcha caps/JFEJ5.PNG                                    OK 
Extracting  captcha caps/JGXV6.PNG                                    OK 
Extracting  captcha caps/K2K9D.PNG                                    OK 
Extracting  captcha caps/K4GBD.PNG                                    OK 
Extracting  captcha caps/K5QCD.PNG                                    OK 
Extracting  captcha caps/K6U7U.PNG                                    OK 
Extracting  captcha caps/KCXAC.PNG                                    OK 
Extracting  captcha caps/KFBTD.PNG                                    OK 
Extracting  captcha caps/KG4HH.PNG                                    OK 
Extracting  captcha caps/KHZE4.PNG                                    OK 
Extracting  captcha caps/KSJ4A.PNG                                    OK 
Extracting  captcha caps/KZ62P.PNG                                    OK 
Extracting  captcha caps/L345J.PNG                                    OK 
Extracting  captcha caps/L36HP.PNG                                    OK 
Extracting  captcha caps/L37HK.PNG                                    OK 
Extracting  captcha caps/L9EG2.PNG                                    OK 
Extracting  captcha caps/L9H4D.PNG                                    OK 
Extracting  captcha caps/LA4P7.PNG                                    OK 
Extracting  captcha caps/LBNXN.PNG                                    OK 
Extracting  captcha caps/LJ6PC.PNG                                    OK 
Extracting  captcha caps/LJ7DT.PNG                                    OK 
Extracting  captcha caps/LVRKL.PNG                                    OK 
Extracting  captcha caps/LXFBT.PNG                                    OK 
Extracting  captcha caps/N52PE.PNG                                    OK 
Extracting  captcha caps/N8ZFS.PNG                                    OK 
Extracting  captcha caps/NBHG7.PNG                                    OK 
Extracting  captcha caps/NGGQ.PNG                                     OK 
Extracting  captcha caps/NHRGB.PNG                                    OK 
Extracting  captcha caps/NKJX4.PNG                                    OK 
Extracting  captcha caps/NPPPD.PNG                                    OK 
Extracting  captcha caps/NU4KH.PNG                                    OK 
Extracting  captcha caps/NX7BX.PNG                                    OK 
Extracting  captcha caps/P2K69.PNG                                    OK 
Extracting  captcha caps/PFHLR.PNG                                    OK 
Extracting  captcha caps/PTFPU.PNG                                    OK 
Extracting  captcha caps/PV6YU.PNG                                    OK 
Extracting  captcha caps/Q4KXA.PNG                                    OK 
Extracting  captcha caps/Q6L72.PNG                                    OK 
Extracting  captcha caps/Q7HNQ.PNG                                    OK 
Extracting  captcha caps/Q8A2V.PNG                                    OK 
Extracting  captcha caps/QC7TD.PNG                                    OK 
Extracting  captcha caps/QEYHL.PNG                                    OK 
Extracting  captcha caps/QHHKX.PNG                                    OK 
Extracting  captcha caps/QRRE2.PNG                                    OK 
Extracting  captcha caps/QU3JN.PNG                                    OK 
Extracting  captcha caps/QURPC.PNG                                    OK 
Extracting  captcha caps/QZR3Z.PNG                                    OK 
Extracting  captcha caps/R27UF.PNG                                    OK 
Extracting  captcha caps/R38UE.PNG                                    OK 
Extracting  captcha caps/R3UVK.PNG                                    OK 
Extracting  captcha caps/RDE9N.PNG                                    OK 
Extracting  captcha caps/RJUNG.PNG                                    OK 
Extracting  captcha caps/RJYRY.PNG                                    OK 
Extracting  captcha caps/RLKT2.PNG                                    OK 
Extracting  captcha caps/RLL8T.PNG                                    OK 
Extracting  captcha caps/RLTV6.PNG                                    OK 
Extracting  captcha caps/RQ2QT.PNG                                    OK 
Extracting  captcha caps/RQZST.PNG                                    OK 
Extracting  captcha caps/RU5HH.PNG                                    OK 
Extracting  captcha caps/RZFFE.PNG                                    OK 
Extracting  captcha caps/S2Y25.PNG                                    OK 
Extracting  captcha caps/SG9EL.PNG                                    OK 
Extracting  captcha caps/SHRTS.PNG                                    OK 
Extracting  captcha caps/SJ6KJ.PNG                                    OK 
Extracting  captcha caps/SPHR4.PNG                                    OK 
Extracting  captcha caps/SQ7S2.PNG                                    OK 
Extracting  captcha caps/SQDUP.PNG                                    OK 
Extracting  captcha caps/SVYGE.PNG                                    OK 
Extracting  captcha caps/T3HA6.PNG                                    OK 
Extracting  captcha caps/T49RZ.PNG                                    OK 
Extracting  captcha caps/TJ6DE.PNG                                    OK 
Extracting  captcha caps/TNVRU.PNG                                    OK 
Extracting  captcha caps/TR9L8.PNG                                    OK 
Extracting  captcha caps/TTBYT.PNG                                    OK 
Extracting  captcha caps/TUJZK.PNG                                    OK 
Extracting  captcha caps/TVAKL.PNG                                    OK 
Extracting  captcha caps/U554X.PNG                                    OK 
Extracting  captcha caps/U7SKY.PNG                                    OK 
Extracting  captcha caps/U8U4N.PNG                                    OK 
Extracting  captcha caps/UDB3Z.PNG                                    OK 
Extracting  captcha caps/UDD86.PNG                                    OK 
Extracting  captcha caps/UDTQS.PNG                                    OK 
Extracting  captcha caps/UHB6B.PNG                                    OK 
Extracting  captcha caps/UKSL7.PNG                                    OK 
Extracting  captcha caps/UP88V.PNG                                    OK 
Extracting  captcha caps/URCF5.PNG                                    OK 
Extracting  captcha caps/URKGY.PNG                                    OK 
Extracting  captcha caps/URQ7D.PNG                                    OK 
Extracting  captcha caps/UTYPV.PNG                                    OK 
Extracting  captcha caps/UYY55.PNG                                    OK 
Extracting  captcha caps/V99ZG.PNG                                    OK 
Extracting  captcha caps/VB93G.PNG                                    OK 
Extracting  captcha caps/VX43Z.PNG                                    OK 
Extracting  captcha caps/VYLHE.PNG                                    OK 
Extracting  captcha caps/X9PJE.PNG                                    OK 
Extracting  captcha caps/XC93Q.PNG                                    OK 
Extracting  captcha caps/XETJ6.PNG                                    OK 
Extracting  captcha caps/XGHYS.PNG                                    OK 
Extracting  captcha caps/XGSHP.PNG                                    OK 
Extracting  captcha caps/XH7NL.PNG                                    OK 
Extracting  captcha caps/XHYGL.PNG                                    OK 
Extracting  captcha caps/XPQ6V.PNG                                    OK 
Extracting  captcha caps/XTC2K.PNG                                    OK 
Extracting  captcha caps/XVFC7.PNG                                    OK 
Extracting  captcha caps/XY364.PNG                                    OK 
Extracting  captcha caps/Y2KFQ.PNG                                    OK 
Extracting  captcha caps/Y5HRR.PNG                                    OK 
Extracting  captcha caps/Y9R52.PNG                                    OK 
Extracting  captcha caps/YC6TY.PNG                                    OK 
Extracting  captcha caps/YE6KN.PNG                                    OK 
Extracting  captcha caps/YGNG.PNG                                     OK 
Extracting  captcha caps/YJ433.PNG                                    OK 
Extracting  captcha caps/YJPSF.PNG                                    OK 
Extracting  captcha caps/YL647.PNG                                    OK 
Extracting  captcha caps/YNDAP.PNG                                    OK 
Extracting  captcha caps/YPJAT.PNG                                    OK 
Extracting  captcha caps/YTZLF.PNG                                    OK 
Extracting  captcha caps/YU63S.PNG                                    OK 
Extracting  captcha caps/YVX5X.PNG                                    OK 
Extracting  captcha caps/YXK3G.PNG                                    OK 
Extracting  captcha caps/YYGQ2.PNG                                    OK 
Extracting  captcha caps/Z3EQP.PNG                                    OK 
Extracting  captcha caps/Z79JR.PNG                                    OK 
Extracting  captcha caps/ZBJHX.PNG                                    OK 
Extracting  captcha caps/ZCUJB.PNG                                    OK 
Extracting  captcha caps/ZEEBR.PNG                                    OK 
Extracting  captcha caps/ZENDD.PNG                                    OK 
Extracting  captcha caps/ZQPTE.PNG                                    OK 
Extracting  captcha caps/ZYHPX.PNG                                    OK 
Extracting  captcha caps/ZYJ6X.PNG                                    OK 
All OK
Number of images found:  300
Number of labels found:  300
Number of unique characters:  30
Characters present:  {'9', 'A', '6', 'Y', '2', 'J', '3', 'F', 'C', 'X', 'D', 'N', 'V', '4', '5', 'K', 'Z', '7', 'P', 'L', 'H', 'T', 'Q', 'E', 'U', '8', 'S', 'G', 'R', 'B'}
-----------------------
!pip install unrar
!unrar x /content/captcha_caps.rar

import os
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path
from collections import Counter
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

data_dir = Path("/content/captcha_caps/")

images = sorted(list(map(str, list(data_dir.glob("*.PNG")))))
labels = [img.split(os.path.sep)[-1].split(".PNG")[0] for img in images]
characters = set(char for label in labels for char in label)

print("Number of images found: ", len(images))
print("Number of labels found: ", len(labels))
print("Number of unique characters: ", len(characters))
print("Characters present: ", characters)

batch_size = 16

img_width = 200
img_height = 50

downsample_factor = 4

max_length = max([len(label) for label in labels])
char_to_num = layers.StringLookup(
    vocabulary=list(characters), mask_token=None
)

num_to_char = layers.StringLookup(
    vocabulary=char_to_num.get_vocabulary(), mask_token=None, invert=True
)

def split_data(images, labels, train_size=0.9, shuffle=True):
    size = len(images)
    indices = np.arange(size)
    if shuffle:
        np.random.shuffle(indices)
    train_samples = int(size * train_size)
    x_train, y_train = images[indices[:train_samples]], labels[indices[:train_samples]]
    x_valid, y_valid = images[indices[train_samples:]], labels[indices[train_samples:]]
    return x_train, x_valid, y_train, y_valid

x_train, x_valid, y_train, y_valid = split_data(np.array(images), np.array(labels))


def encode_single_sample(img_path, label):
    img = tf.io.read_file(img_path)
    img = tf.io.decode_png(img, channels=1)
    img = tf.image.convert_image_dtype(img, tf.float32)
    img = tf.image.resize(img, [img_height, img_width])
    img = tf.transpose(img, perm=[1, 0, 2])
    label = char_to_num(tf.strings.unicode_split(label, input_encoding="UTF-8"))
    return {"image": img, "label": label}

train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_dataset = (
    train_dataset.map(
        encode_single_sample, num_parallel_calls=tf.data.AUTOTUNE
    )
    .batch(batch_size)
    .prefetch(buffer_size=tf.data.AUTOTUNE).repeat(10)
)

validation_dataset = tf.data.Dataset.from_tensor_slices((x_valid, y_valid))
validation_dataset = (
    validation_dataset.map(
        encode_single_sample, num_parallel_calls=tf.data.AUTOTUNE
    )
    .batch(batch_size)
    .prefetch(buffer_size=tf.data.AUTOTUNE)
)

_, ax = plt.subplots(4, 4, figsize=(10, 5))
for batch in validation_dataset.take(1):
    images = batch["image"]
    labels = batch["label"]
    for i in range(16):
        img = (images[i] * 255).numpy().astype("uint8")
        label = tf.strings.reduce_join(num_to_char(labels[i])).numpy().decode("utf-8")
        ax[i // 4, i % 4].imshow(img[:, :, 0].T, cmap="gray")
        ax[i // 4, i % 4].set_title(label)
        ax[i // 4, i % 4].axis("off")
plt.show()
Requirement already satisfied: unrar in /usr/local/lib/python3.7/dist-packages (0.4)

UNRAR 5.50 freeware      Copyright (c) 1993-2017 Alexander Roshal


Extracting from /content/captcha_caps.rar

Creating    captcha caps                                              OK
Extracting  captcha caps/24VCZ.PNG                                    OK 
Extracting  captcha caps/26SGX.PNG                                    OK 
Extracting  captcha caps/2HC5E.PNG                                    OK 
Extracting  captcha caps/2NDXL.PNG                                    OK 
Extracting  captcha caps/2NUEH.PNG                                    OK 
Extracting  captcha caps/2QX4B.PNG                                    OK 
Extracting  captcha caps/2V78Y.PNG                                    OK 
Extracting  captcha caps/2Z45Y.PNG                                    OK 
Extracting  captcha caps/2Z9R2.PNG                                    OK 
Extracting  captcha caps/32HZA.PNG                                    OK 
Extracting  captcha caps/38JKT.PNG                                    OK 
Extracting  captcha caps/39EZ4.PNG                                    OK 
Extracting  captcha caps/3GJ85.PNG                                    OK 
Extracting  captcha caps/3R2JE.PNG                                    OK 
Extracting  captcha caps/3RU4C.PNG                                    OK 
Extracting  captcha caps/3TPFA.PNG                                    OK 
Extracting  captcha caps/3TVAC.PNG                                    OK 
Extracting  captcha caps/44U8C.PNG                                    OK 
Extracting  captcha caps/452LV.PNG                                    OK 
Extracting  captcha caps/4E4P8.PNG                                    OK 
Extracting  captcha caps/4E5HX.PNG                                    OK 
Extracting  captcha caps/4FVS7.PNG                                    OK 
Extracting  captcha caps/4GJCC.PNG                                    OK 
Extracting  captcha caps/4QQJD.PNG                                    OK 
Extracting  captcha caps/4TH2K.PNG                                    OK 
Extracting  captcha caps/4TN2L.PNG                                    OK 
Extracting  captcha caps/4YBT5.PNG                                    OK 
Extracting  captcha caps/4ZLHE.PNG                                    OK 
Extracting  captcha caps/556F5.PNG                                    OK 
Extracting  captcha caps/55DT5.PNG                                    OK 
Extracting  captcha caps/5CEZD.PNG                                    OK 
Extracting  captcha caps/5CQ39.PNG                                    OK 
Extracting  captcha caps/5FZUR.PNG                                    OK 
Extracting  captcha caps/5H7F4.PNG                                    OK 
Extracting  captcha caps/5K4TY.PNG                                    OK 
Extracting  captcha caps/5N2KC.PNG                                    OK 
Extracting  captcha caps/5P6B4.PNG                                    OK 
Extracting  captcha caps/5R728.PNG                                    OK 
Extracting  captcha caps/5S9E7.PNG                                    OK 
Extracting  captcha caps/5VRRV.PNG                                    OK 
Extracting  captcha caps/5VZHL.PNG                                    OK 
Extracting  captcha caps/5YVYG.PNG                                    OK 
Extracting  captcha caps/63P4N.PNG                                    OK 
Extracting  captcha caps/65DQ7.PNG                                    OK 
Extracting  captcha caps/66JUU.PNG                                    OK 
Extracting  captcha caps/69ZQ3.PNG                                    OK 
Extracting  captcha caps/6B655.PNG                                    OK 
Extracting  captcha caps/6GBFG.PNG                                    OK 
Extracting  captcha caps/6K27H.PNG                                    OK 
Extracting  captcha caps/6R7G5.PNG                                    OK 
Extracting  captcha caps/6VFYG.PNG                                    OK 
Extracting  captcha caps/6X8AJ.PNG                                    OK 
Extracting  captcha caps/6ZNJP.PNG                                    OK 
Extracting  captcha caps/73ZK2.PNG                                    OK 
Extracting  captcha caps/74FPR.PNG                                    OK 
Extracting  captcha caps/7C46N.PNG                                    OK 
Extracting  captcha caps/7C48B.PNG                                    OK 
Extracting  captcha caps/7JVBT.PNG                                    OK 
Extracting  captcha caps/7NVS8.PNG                                    OK 
Extracting  captcha caps/7REZP.PNG                                    OK 
Extracting  captcha caps/7RHSQ.PNG                                    OK 
Extracting  captcha caps/7RTT2.PNG                                    OK 
Extracting  captcha caps/7VV9J.PNG                                    OK 
Extracting  captcha caps/82JNK.PNG                                    OK 
Extracting  captcha caps/83JKQ.PNG                                    OK 
Extracting  captcha caps/89RGK.PNG                                    OK 
Extracting  captcha caps/8A2D7.PNG                                    OK 
Extracting  captcha caps/8ENGQ.PNG                                    OK 
Extracting  captcha caps/8K5KS.PNG                                    OK 
Extracting  captcha caps/95BDX.PNG                                    OK 
Extracting  captcha caps/963D9.PNG                                    OK 
Extracting  captcha caps/9878H.PNG                                    OK 
Extracting  captcha caps/99G9R.PNG                                    OK 
Extracting  captcha caps/99RJ8.PNG                                    OK 
Extracting  captcha caps/9CKGT.PNG                                    OK 
Extracting  captcha caps/9DK36.PNG                                    OK 
Extracting  captcha caps/9E3FU.PNG                                    OK 
Extracting  captcha caps/9EZCJ.PNG                                    OK 
Extracting  captcha caps/9HS3T.PNG                                    OK 
Extracting  captcha caps/9J59G.PNG                                    OK 
Extracting  captcha caps/9JXEJ.PNG                                    OK 
Extracting  captcha caps/9TBBF.PNG                                    OK 
Extracting  captcha caps/9TYDP.PNG                                    OK 
Extracting  captcha caps/9YEY2.PNG                                    OK 
Extracting  captcha caps/A6TC6.PNG                                    OK 
Extracting  captcha caps/ADB8Y.PNG                                    OK 
Extracting  captcha caps/AERBR.PNG                                    OK 
Extracting  captcha caps/AG43G.PNG                                    OK 
Extracting  captcha caps/ALX5Q.PNG                                    OK 
Extracting  captcha caps/AP6EJ.PNG                                    OK 
Extracting  captcha caps/AUFH4.PNG                                    OK 
Extracting  captcha caps/AVAYP.PNG                                    OK 
Extracting  captcha caps/AX2QR.PNG                                    OK 
Extracting  captcha caps/AZS3U.PNG                                    OK 
Extracting  captcha caps/B6ZYP.PNG                                    OK 
Extracting  captcha caps/B8YTF.PNG                                    OK 
Extracting  captcha caps/BEGC2.PNG                                    OK 
Extracting  captcha caps/BQFXZ.PNG                                    OK 
Extracting  captcha caps/BQSB2.PNG                                    OK 
Extracting  captcha caps/BT5CN.PNG                                    OK 
Extracting  captcha caps/BYJL9.PNG                                    OK 
Extracting  captcha caps/BZYB7.PNG                                    OK 
Extracting  captcha caps/C2EFS.PNG                                    OK 
Extracting  captcha caps/C3T9L.PNG                                    OK 
Extracting  captcha caps/C8C26.PNG                                    OK 
Extracting  captcha caps/CACQC.PNG                                    OK 
Extracting  captcha caps/CBXJY.PNG                                    OK 
Extracting  captcha caps/CE6S8.PNG                                    OK 
Extracting  captcha caps/CEFCR.PNG                                    OK 
Extracting  captcha caps/CEPQV.PNG                                    OK 
Extracting  captcha caps/CF3V8.PNG                                    OK 
Extracting  captcha caps/CFR3R.PNG                                    OK 
Extracting  captcha caps/CKEQK.PNG                                    OK 
Extracting  captcha caps/CUD8R.PNG                                    OK 
Extracting  captcha caps/D2ZSU.PNG                                    OK 
Extracting  captcha caps/D56EX.PNG                                    OK 
Extracting  captcha caps/DBAAX.PNG                                    OK 
Extracting  captcha caps/DC2AV.PNG                                    OK 
Extracting  captcha caps/DDZRZ.PNG                                    OK 
Extracting  captcha caps/DF266.PNG                                    OK 
Extracting  captcha caps/DGLYX.PNG                                    OK 
Extracting  captcha caps/DNQ8C.PNG                                    OK 
Extracting  captcha caps/DPQCC.PNG                                    OK 
Extracting  captcha caps/DUU3R.PNG                                    OK 
Extracting  captcha caps/DY935.PNG                                    OK 
Extracting  captcha caps/DYE9U.PNG                                    OK 
Extracting  captcha caps/E6RVE.PNG                                    OK 
Extracting  captcha caps/E7B47.PNG                                    OK 
Extracting  captcha caps/EB975.PNG                                    OK 
Extracting  captcha caps/EHQVT.PNG                                    OK 
Extracting  captcha caps/EJB7K.PNG                                    OK 
Extracting  captcha caps/EJEUJ.PNG                                    OK 
Extracting  captcha caps/EN3SG.PNG                                    OK 
Extracting  captcha caps/EQP2Q.PNG                                    OK 
Extracting  captcha caps/ESLUT.PNG                                    OK 
Extracting  captcha caps/ET497.PNG                                    OK 
Extracting  captcha caps/F2GTJ.PNG                                    OK 
Extracting  captcha caps/F32UK.PNG                                    OK 
Extracting  captcha caps/F8B56.PNG                                    OK 
Extracting  captcha caps/FEQRA.PNG                                    OK 
Extracting  captcha caps/FF5AZ.png                                    OK 
Extracting  captcha caps/FGBBV.PNG                                    OK 
Extracting  captcha caps/FN4XQ.PNG                                    OK 
Extracting  captcha caps/FUHZJ.PNG                                    OK 
Extracting  captcha caps/FZBZB.PNG                                    OK 
Extracting  captcha caps/G3C7R.PNG                                    OK 
Extracting  captcha caps/G3H2V.PNG                                    OK 
Extracting  captcha caps/G5A9V.PNG                                    OK 
Extracting  captcha caps/G9CLN.PNG                                    OK 
Extracting  captcha caps/GAGZG.PNG                                    OK 
Extracting  captcha caps/GCRFA.PNG                                    OK 
Extracting  captcha caps/GF59Q.PNG                                    OK 
Extracting  captcha caps/GGFJH.PNG                                    OK 
Extracting  captcha caps/GHNPE.PNG                                    OK 
Extracting  captcha caps/GKGQ5.PNG                                    OK 
Extracting  captcha caps/GLNPR.PNG                                    OK 
Extracting  captcha caps/GRKU3.PNG                                    OK 
Extracting  captcha caps/GXGEA.PNG                                    OK 
Extracting  captcha caps/H2JTQ.PNG                                    OK 
Extracting  captcha caps/HAF8J.PNG                                    OK 
Extracting  captcha caps/HDK86.PNG                                    OK 
Extracting  captcha caps/HLG6G.PNG                                    OK 
Extracting  captcha caps/HQEFG.PNG                                    OK 
Extracting  captcha caps/HUG3K.PNG                                    OK 
Extracting  captcha caps/HZBJ9.PNG                                    OK 
Extracting  captcha caps/J38T7.PNG                                    OK 
Extracting  captcha caps/J3DG2.PNG                                    OK 
Extracting  captcha caps/J5PE7.PNG                                    OK 
Extracting  captcha caps/J72PD.PNG                                    OK 
Extracting  captcha caps/J8E5Z.PNG                                    OK 
Extracting  captcha caps/JC5YF.PNG                                    OK 
Extracting  captcha caps/JFEJ5.PNG                                    OK 
Extracting  captcha caps/JGXV6.PNG                                    OK 
Extracting  captcha caps/K2K9D.PNG                                    OK 
Extracting  captcha caps/K4GBD.PNG                                    OK 
Extracting  captcha caps/K5QCD.PNG                                    OK 
Extracting  captcha caps/K6U7U.PNG                                    OK 
Extracting  captcha caps/KCXAC.PNG                                    OK 
Extracting  captcha caps/KFBTD.PNG                                    OK 
Extracting  captcha caps/KG4HH.PNG                                    OK 
Extracting  captcha caps/KHZE4.PNG                                    OK 
Extracting  captcha caps/KSJ4A.PNG                                    OK 
Extracting  captcha caps/KZ62P.PNG                                    OK 
Extracting  captcha caps/L345J.PNG                                    OK 
Extracting  captcha caps/L36HP.PNG                                    OK 
Extracting  captcha caps/L37HK.PNG                                    OK 
Extracting  captcha caps/L9EG2.PNG                                    OK 
Extracting  captcha caps/L9H4D.PNG                                    OK 
Extracting  captcha caps/LA4P7.PNG                                    OK 
Extracting  captcha caps/LBNXN.PNG                                    OK 
Extracting  captcha caps/LJ6PC.PNG                                    OK 
Extracting  captcha caps/LJ7DT.PNG                                    OK 
Extracting  captcha caps/LVRKL.PNG                                    OK 
Extracting  captcha caps/LXFBT.PNG                                    OK 
Extracting  captcha caps/N52PE.PNG                                    OK 
Extracting  captcha caps/N8ZFS.PNG                                    OK 
Extracting  captcha caps/NBHG7.PNG                                    OK 
Extracting  captcha caps/NGGQ.PNG                                     OK 
Extracting  captcha caps/NHRGB.PNG                                    OK 
Extracting  captcha caps/NKJX4.PNG                                    OK 
Extracting  captcha caps/NPPPD.PNG                                    OK 
Extracting  captcha caps/NU4KH.PNG                                    OK 
Extracting  captcha caps/NX7BX.PNG                                    OK 
Extracting  captcha caps/P2K69.PNG                                    OK 
Extracting  captcha caps/PFHLR.PNG                                    OK 
Extracting  captcha caps/PTFPU.PNG                                    OK 
Extracting  captcha caps/PV6YU.PNG                                    OK 
Extracting  captcha caps/Q4KXA.PNG                                    OK 
Extracting  captcha caps/Q6L72.PNG                                    OK 
Extracting  captcha caps/Q7HNQ.PNG                                    OK 
Extracting  captcha caps/Q8A2V.PNG                                    OK 
Extracting  captcha caps/QC7TD.PNG                                    OK 
Extracting  captcha caps/QEYHL.PNG                                    OK 
Extracting  captcha caps/QHHKX.PNG                                    OK 
Extracting  captcha caps/QRRE2.PNG                                    OK 
Extracting  captcha caps/QU3JN.PNG                                    OK 
Extracting  captcha caps/QURPC.PNG                                    OK 
Extracting  captcha caps/QZR3Z.PNG                                    OK 
Extracting  captcha caps/R27UF.PNG                                    OK 
Extracting  captcha caps/R38UE.PNG                                    OK 
Extracting  captcha caps/R3UVK.PNG                                    OK 
Extracting  captcha caps/RDE9N.PNG                                    OK 
Extracting  captcha caps/RJUNG.PNG                                    OK 
Extracting  captcha caps/RJYRY.PNG                                    OK 
Extracting  captcha caps/RLKT2.PNG                                    OK 
Extracting  captcha caps/RLL8T.PNG                                    OK 
Extracting  captcha caps/RLTV6.PNG                                    OK 
Extracting  captcha caps/RQ2QT.PNG                                    OK 
Extracting  captcha caps/RQZST.PNG                                    OK 
Extracting  captcha caps/RU5HH.PNG                                    OK 
Extracting  captcha caps/RZFFE.PNG                                    OK 
Extracting  captcha caps/S2Y25.PNG                                    OK 
Extracting  captcha caps/SG9EL.PNG                                    OK 
Extracting  captcha caps/SHRTS.PNG                                    OK 
Extracting  captcha caps/SJ6KJ.PNG                                    OK 
Extracting  captcha caps/SPHR4.PNG                                    OK 
Extracting  captcha caps/SQ7S2.PNG                                    OK 
Extracting  captcha caps/SQDUP.PNG                                    OK 
Extracting  captcha caps/SVYGE.PNG                                    OK 
Extracting  captcha caps/T3HA6.PNG                                    OK 
Extracting  captcha caps/T49RZ.PNG                                    OK 
Extracting  captcha caps/TJ6DE.PNG                                    OK 
Extracting  captcha caps/TNVRU.PNG                                    OK 
Extracting  captcha caps/TR9L8.PNG                                    OK 
Extracting  captcha caps/TTBYT.PNG                                    OK 
Extracting  captcha caps/TUJZK.PNG                                    OK 
Extracting  captcha caps/TVAKL.PNG                                    OK 
Extracting  captcha caps/U554X.PNG                                    OK 
Extracting  captcha caps/U7SKY.PNG                                    OK 
Extracting  captcha caps/U8U4N.PNG                                    OK 
Extracting  captcha caps/UDB3Z.PNG                                    OK 
Extracting  captcha caps/UDD86.PNG                                    OK 
Extracting  captcha caps/UDTQS.PNG                                    OK 
Extracting  captcha caps/UHB6B.PNG                                    OK 
Extracting  captcha caps/UKSL7.PNG                                    OK 
Extracting  captcha caps/UP88V.PNG                                    OK 
Extracting  captcha caps/URCF5.PNG                                    OK 
Extracting  captcha caps/URKGY.PNG                                    OK 
Extracting  captcha caps/URQ7D.PNG                                    OK 
Extracting  captcha caps/UTYPV.PNG                                    OK 
Extracting  captcha caps/UYY55.PNG                                    OK 
Extracting  captcha caps/V99ZG.PNG                                    OK 
Extracting  captcha caps/VB93G.PNG                                    OK 
Extracting  captcha caps/VX43Z.PNG                                    OK 
Extracting  captcha caps/VYLHE.PNG                                    OK 
Extracting  captcha caps/X9PJE.PNG                                    OK 
Extracting  captcha caps/XC93Q.PNG                                    OK 
Extracting  captcha caps/XETJ6.PNG                                    OK 
Extracting  captcha caps/XGHYS.PNG                                    OK 
Extracting  captcha caps/XGSHP.PNG                                    OK 
Extracting  captcha caps/XH7NL.PNG                                    OK 
Extracting  captcha caps/XHYGL.PNG                                    OK 
Extracting  captcha caps/XPQ6V.PNG                                    OK 
Extracting  captcha caps/XTC2K.PNG                                    OK 
Extracting  captcha caps/XVFC7.PNG                                    OK 
Extracting  captcha caps/XY364.PNG                                    OK 
Extracting  captcha caps/Y2KFQ.PNG                                    OK 
Extracting  captcha caps/Y5HRR.PNG                                    OK 
Extracting  captcha caps/Y9R52.PNG                                    OK 
Extracting  captcha caps/YC6TY.PNG                                    OK 
Extracting  captcha caps/YE6KN.PNG                                    OK 
Extracting  captcha caps/YGNG.PNG                                     OK 
Extracting  captcha caps/YJ433.PNG                                    OK 
Extracting  captcha caps/YJPSF.PNG                                    OK 
Extracting  captcha caps/YL647.PNG                                    OK 
Extracting  captcha caps/YNDAP.PNG                                    OK 
Extracting  captcha caps/YPJAT.PNG                                    OK 
Extracting  captcha caps/YTZLF.PNG                                    OK 
Extracting  captcha caps/YU63S.PNG                                    OK 
Extracting  captcha caps/YVX5X.PNG                                    OK 
Extracting  captcha caps/YXK3G.PNG                                    OK 
Extracting  captcha caps/YYGQ2.PNG                                    OK 
Extracting  captcha caps/Z3EQP.PNG                                    OK 
Extracting  captcha caps/Z79JR.PNG                                    OK 
Extracting  captcha caps/ZBJHX.PNG                                    OK 
Extracting  captcha caps/ZCUJB.PNG                                    OK 
Extracting  captcha caps/ZEEBR.PNG                                    OK 
Extracting  captcha caps/ZENDD.PNG                                    OK 
Extracting  captcha caps/ZQPTE.PNG                                    OK 
Extracting  captcha caps/ZYHPX.PNG                                    OK 
Extracting  captcha caps/ZYJ6X.PNG                                    OK 
All OK
Number of images found:  300
Number of labels found:  300
Number of unique characters:  30
Characters present:  {'9', 'A', '6', 'Y', '2', 'J', '3', 'F', 'C', 'X', 'D', 'N', 'V', '4', '5', 'K', 'Z', '7', 'P', 'L', 'H', 'T', 'Q', 'E', 'U', '8', 'S', 'G', 'R', 'B'}

Array index loop to begin instead of mem access error

copy iconCopydownload iconDownload
long a[4];
size_t sz = sizeof(a) / sizeof(long);

for (size_t i = 0; i < sz; i++)
{
    if (a[i] && a[(i + 1) % sz]) //Define some conditions regarding a[i] and a[i+1]
    {
        //Process code
    }
}
for (size_t i = 0; i < sz; i++)
{
    size_t j = i < sz-1 ? i+1 : 0;
    if (a[i] && a[j]) //Define some conditions regarding a[i] and a[i+1]
    {
        //Process code
    }
}
-----------------------
long a[4];
size_t sz = sizeof(a) / sizeof(long);

for (size_t i = 0; i < sz; i++)
{
    if (a[i] && a[(i + 1) % sz]) //Define some conditions regarding a[i] and a[i+1]
    {
        //Process code
    }
}
for (size_t i = 0; i < sz; i++)
{
    size_t j = i < sz-1 ? i+1 : 0;
    if (a[i] && a[j]) //Define some conditions regarding a[i] and a[i+1]
    {
        //Process code
    }
}

Swift's Vision framework not recognizing Japanese characters

copy iconCopydownload iconDownload
["en-US", "fr-FR", "it-IT", "de-DE", "es-ES", "pt-BR", "zh-Hans", "zh-Hant"]
["en-US", "fr-FR", "it-IT", "de-DE", "es-ES", "pt-BR"]
["en-US"]
-----------------------
["en-US", "fr-FR", "it-IT", "de-DE", "es-ES", "pt-BR", "zh-Hans", "zh-Hant"]
["en-US", "fr-FR", "it-IT", "de-DE", "es-ES", "pt-BR"]
["en-US"]
-----------------------
["en-US", "fr-FR", "it-IT", "de-DE", "es-ES", "pt-BR", "zh-Hans", "zh-Hant"]
["en-US", "fr-FR", "it-IT", "de-DE", "es-ES", "pt-BR"]
["en-US"]

Community Discussions

Trending Discussions on vision
  • Camera calibration, focal length value seems too large
  • React-native-vision-camera can't access to normal camera in back
  • Create new boolean fields based on specific bigrams appearing in a tokenized pandas dataframe
  • How do I remove background from an image like this?
  • After updating Gradle to 7.0.2, Element type “manifest” must be followed by either attribute specifications, “&gt;” or “/&gt;” error
  • Combining Object Detection with Text to Speech Code
  • Android Huawei image segmentation not working on release build
  • How to get post categories from an object in array
  • How do you create a new AVAsset video that consists of only frames from given `CMTimeRange`s of another video?
  • How do you implement React-native-gesture-handler with React Navigation 6.x Native Stack Navigator (RN&gt;0.6)?
Trending Discussions on vision

QUESTION

Camera calibration, focal length value seems too large

Asked 2022-Mar-16 at 16:58

I tried a camera calibration with python and opencv to find the camera matrix. I used the following code from this link

https://automaticaddison.com/how-to-perform-camera-calibration-using-opencv/

import cv2 # Import the OpenCV library to enable computer vision
import numpy as np # Import the NumPy scientific computing library
import glob # Used to get retrieve files that have a specified pattern
 
# Path to the image that you want to undistort
distorted_img_filename = r'C:\Users\uid20832\3.jpg'
 
# Chessboard dimensions
number_of_squares_X = 10 # Number of chessboard squares along the x-axis
number_of_squares_Y = 7  # Number of chessboard squares along the y-axis
nX = number_of_squares_X - 1 # Number of interior corners along x-axis
nY = number_of_squares_Y - 1 # Number of interior corners along y-axis
 
# Store vectors of 3D points for all chessboard images (world coordinate frame)
object_points = []
 
# Store vectors of 2D points for all chessboard images (camera coordinate frame)
image_points = []
 
# Set termination criteria. We stop either when an accuracy is reached or when
# we have finished a certain number of iterations.
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
 
# Define real world coordinates for points in the 3D coordinate frame
# Object points are (0,0,0), (1,0,0), (2,0,0) ...., (5,8,0)
object_points_3D = np.zeros((nX * nY, 3), np.float32)       
 
# These are the x and y coordinates                                              
object_points_3D[:,:2] = np.mgrid[0:nY, 0:nX].T.reshape(-1, 2) 
 
def main():
     
  # Get the file path for images in the current directory
  images = glob.glob(r'C:\Users\Kalibrierung\*.jpg')
     
  # Go through each chessboard image, one by one
  for image_file in images:
  
    # Load the image
    image = cv2.imread(image_file)  
 
    # Convert the image to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  
 
    # Find the corners on the chessboard
    success, corners = cv2.findChessboardCorners(gray, (nY, nX), None)
     
    # If the corners are found by the algorithm, draw them
    if success == True:
 
      # Append object points
      object_points.append(object_points_3D)
 
      # Find more exact corner pixels       
      corners_2 = cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)       
       
            # Append image points
      image_points.append(corners)
 
      # Draw the corners
      cv2.drawChessboardCorners(image, (nY, nX), corners_2, success)
 
      # Display the image. Used for testing.
      #cv2.imshow("Image", image) 
     
      # Display the window for a short period. Used for testing.
      #cv2.waitKey(200) 
                                                                                                                     
  # Now take a distorted image and undistort it 
  distorted_image = cv2.imread(distorted_img_filename)
 
  # Perform camera calibration to return the camera matrix, distortion coefficients, rotation and translation vectors etc 
  ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(object_points, 
                                                    image_points, 
                                                    gray.shape[::-1], 
                                                    None, 
                                                    None)

But I think I always get wrong parameters. My focal length is around 1750 in x and y direction from calibration. I think this couldnt be rigth, it is pretty much. The camera documentation says the focal lentgh is between 4-7 mm. But I am not sure, why it is so high from the calibration. Here are some of my photos for the calibration. Maybe something is wrong with them. I moved the chessboard under the camera in different directions, angles and high.

I was also wondering, why I dont need the size of the squares in the code. Can someone explains it to me or did I forgot this input somewhere?

enter image description here enter image description here enter image description here

ANSWER

Answered 2021-Sep-13 at 11:31

Your misconception is about "focal length". It's an overloaded term.

  • "focal length" (unit mm) in the optical part: it describes the distance between the lens plane and image/sensor plane
  • "focal length" (unit pixels) in the camera matrix: it describes a scale factor for mapping the real world to a picture of a certain resolution

1750 may very well be correct, if you have a high resolution picture (Full HD or something).

The calculation goes:

f [pixels] = (focal length [mm]) / (pixel pitch [µm / pixel])

(take care of the units and prefixes, 1 mm = 1000 µm)

Example: a Pixel 4a phone, which has 1.40 µm pixel pitch and 4.38 mm focal length, has f = ~3128.57 (= fx = fy).

Another example: A Pixel 4a has a diagonal Field of View of approximately 77.7 degrees, and a resolution of 4032 x 3024 pixels, so that's 5040 pixels diagonally. You can calculate:

f = (5040 / 2) / tan(~77.7° / 2)

f = ~3128.6 [pixels]

And that calculation you can apply to arbitrary cameras for which you know the field of view and picture size. Use horizontal FoV and horizontal resolution if the diagonal resolution is ambiguous. That can happen if the sensor isn't 16:9 but the video you take from it is cropped to 16:9... assuming the crop only crops vertically, and leaves the horizontal alone.


Why don't you need the size of the chessboard squares in this code? Because it only calibrates the intrinsic parameters (camera matrix and distortion coefficients). Those don't depend on the distance to the board or any other object in the scene.

If you were to calibrate extrinsic parameters, i.e. the distance of cameras in a stereo setup, then you would need to give the size of the squares.

Source https://stackoverflow.com/questions/69159247

Community Discussions, Code Snippets contain sources that include Stack Exchange Network

Vulnerabilities

No vulnerabilities reported

Install vision

You can download it from GitHub, Maven.
You can use vision like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.

Support

For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .

DOWNLOAD this Library from

Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

Share this Page

share link
Reuse Pre-built Kits with vision
Compare Computer Vision Libraries with Highest Support
Compare Computer Vision Libraries with Highest Security
Compare Computer Vision Libraries with Permissive License
Compare Computer Vision Libraries with Highest Reuse
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

  • © 2022 Open Weaver Inc.